StyleResolver.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/QuickSort.h>
  28. #include <LibWeb/CSS/Parser/CSSParser.h>
  29. #include <LibWeb/CSS/SelectorEngine.h>
  30. #include <LibWeb/CSS/StyleResolver.h>
  31. #include <LibWeb/CSS/StyleRule.h>
  32. #include <LibWeb/CSS/StyleSheet.h>
  33. #include <LibWeb/DOM/Document.h>
  34. #include <LibWeb/DOM/Element.h>
  35. #include <LibWeb/Dump.h>
  36. #include <ctype.h>
  37. #include <stdio.h>
  38. namespace Web::CSS {
  39. StyleResolver::StyleResolver(DOM::Document& document)
  40. : m_document(document)
  41. {
  42. }
  43. StyleResolver::~StyleResolver()
  44. {
  45. }
  46. static StyleSheet& default_stylesheet()
  47. {
  48. static StyleSheet* sheet;
  49. if (!sheet) {
  50. extern const char default_stylesheet_source[];
  51. String css = default_stylesheet_source;
  52. sheet = parse_css(CSS::ParsingContext(), css).leak_ref();
  53. }
  54. return *sheet;
  55. }
  56. static StyleSheet& quirks_mode_stylesheet()
  57. {
  58. static StyleSheet* sheet;
  59. if (!sheet) {
  60. extern const char quirks_mode_stylesheet_source[];
  61. String css = quirks_mode_stylesheet_source;
  62. sheet = parse_css(CSS::ParsingContext(), css).leak_ref();
  63. }
  64. return *sheet;
  65. }
  66. template<typename Callback>
  67. void StyleResolver::for_each_stylesheet(Callback callback) const
  68. {
  69. callback(default_stylesheet());
  70. if (document().in_quirks_mode())
  71. callback(quirks_mode_stylesheet());
  72. for (auto& sheet : document().style_sheets().sheets()) {
  73. callback(sheet);
  74. }
  75. }
  76. Vector<MatchingRule> StyleResolver::collect_matching_rules(const DOM::Element& element) const
  77. {
  78. Vector<MatchingRule> matching_rules;
  79. size_t style_sheet_index = 0;
  80. for_each_stylesheet([&](auto& sheet) {
  81. size_t rule_index = 0;
  82. sheet.for_each_effective_style_rule([&](auto& rule) {
  83. size_t selector_index = 0;
  84. for (auto& selector : rule.selectors()) {
  85. if (SelectorEngine::matches(selector, element)) {
  86. matching_rules.append({ rule, style_sheet_index, rule_index, selector_index });
  87. break;
  88. }
  89. ++selector_index;
  90. }
  91. ++rule_index;
  92. });
  93. ++style_sheet_index;
  94. });
  95. return matching_rules;
  96. }
  97. void StyleResolver::sort_matching_rules(Vector<MatchingRule>& matching_rules) const
  98. {
  99. quick_sort(matching_rules, [&](MatchingRule& a, MatchingRule& b) {
  100. auto& a_selector = a.rule->selectors()[a.selector_index];
  101. auto& b_selector = b.rule->selectors()[b.selector_index];
  102. auto a_specificity = a_selector.specificity();
  103. auto b_specificity = b_selector.specificity();
  104. if (a_selector.specificity() == b_selector.specificity()) {
  105. if (a.style_sheet_index == b.style_sheet_index)
  106. return a.rule_index < b.rule_index;
  107. return a.style_sheet_index < b.style_sheet_index;
  108. }
  109. return a_specificity < b_specificity;
  110. });
  111. }
  112. bool StyleResolver::is_inherited_property(CSS::PropertyID property_id)
  113. {
  114. static HashTable<CSS::PropertyID> inherited_properties;
  115. if (inherited_properties.is_empty()) {
  116. inherited_properties.set(CSS::PropertyID::BorderCollapse);
  117. inherited_properties.set(CSS::PropertyID::BorderSpacing);
  118. inherited_properties.set(CSS::PropertyID::Color);
  119. inherited_properties.set(CSS::PropertyID::FontFamily);
  120. inherited_properties.set(CSS::PropertyID::FontSize);
  121. inherited_properties.set(CSS::PropertyID::FontStyle);
  122. inherited_properties.set(CSS::PropertyID::FontVariant);
  123. inherited_properties.set(CSS::PropertyID::FontWeight);
  124. inherited_properties.set(CSS::PropertyID::LetterSpacing);
  125. inherited_properties.set(CSS::PropertyID::LineHeight);
  126. inherited_properties.set(CSS::PropertyID::ListStyle);
  127. inherited_properties.set(CSS::PropertyID::ListStyleImage);
  128. inherited_properties.set(CSS::PropertyID::ListStylePosition);
  129. inherited_properties.set(CSS::PropertyID::ListStyleType);
  130. inherited_properties.set(CSS::PropertyID::TextAlign);
  131. inherited_properties.set(CSS::PropertyID::TextIndent);
  132. inherited_properties.set(CSS::PropertyID::TextTransform);
  133. inherited_properties.set(CSS::PropertyID::Visibility);
  134. inherited_properties.set(CSS::PropertyID::WhiteSpace);
  135. inherited_properties.set(CSS::PropertyID::WordSpacing);
  136. // FIXME: This property is not supposed to be inherited, but we currently
  137. // rely on inheritance to propagate decorations into line boxes.
  138. inherited_properties.set(CSS::PropertyID::TextDecorationLine);
  139. }
  140. return inherited_properties.contains(property_id);
  141. }
  142. static Vector<String> split_on_whitespace(const StringView& string)
  143. {
  144. if (string.is_empty())
  145. return {};
  146. Vector<String> v;
  147. size_t substart = 0;
  148. for (size_t i = 0; i < string.length(); ++i) {
  149. char ch = string.characters_without_null_termination()[i];
  150. if (isspace(ch)) {
  151. size_t sublen = i - substart;
  152. if (sublen != 0)
  153. v.append(string.substring_view(substart, sublen));
  154. substart = i + 1;
  155. }
  156. }
  157. size_t taillen = string.length() - substart;
  158. if (taillen != 0)
  159. v.append(string.substring_view(substart, taillen));
  160. return v;
  161. }
  162. enum class Edge {
  163. Top,
  164. Right,
  165. Bottom,
  166. Left,
  167. All,
  168. };
  169. static bool contains(Edge a, Edge b)
  170. {
  171. return a == b || b == Edge::All;
  172. }
  173. static inline void set_property_border_width(StyleProperties& style, const StyleValue& value, Edge edge)
  174. {
  175. VERIFY(value.is_length());
  176. if (contains(Edge::Top, edge))
  177. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  178. if (contains(Edge::Right, edge))
  179. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  180. if (contains(Edge::Bottom, edge))
  181. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  182. if (contains(Edge::Left, edge))
  183. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  184. }
  185. static inline void set_property_border_color(StyleProperties& style, const StyleValue& value, Edge edge)
  186. {
  187. VERIFY(value.is_color());
  188. if (contains(Edge::Top, edge))
  189. style.set_property(CSS::PropertyID::BorderTopColor, value);
  190. if (contains(Edge::Right, edge))
  191. style.set_property(CSS::PropertyID::BorderRightColor, value);
  192. if (contains(Edge::Bottom, edge))
  193. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  194. if (contains(Edge::Left, edge))
  195. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  196. }
  197. static inline void set_property_border_style(StyleProperties& style, const StyleValue& value, Edge edge)
  198. {
  199. VERIFY(value.is_string());
  200. if (contains(Edge::Top, edge))
  201. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  202. if (contains(Edge::Right, edge))
  203. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  204. if (contains(Edge::Bottom, edge))
  205. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  206. if (contains(Edge::Left, edge))
  207. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  208. }
  209. static void set_property_expanding_shorthands(StyleProperties& style, CSS::PropertyID property_id, const StyleValue& value, DOM::Document& document)
  210. {
  211. CSS::ParsingContext context(document);
  212. if (property_id == CSS::PropertyID::TextDecoration) {
  213. switch (value.to_identifier()) {
  214. case CSS::ValueID::None:
  215. case CSS::ValueID::Underline:
  216. case CSS::ValueID::Overline:
  217. case CSS::ValueID::LineThrough:
  218. case CSS::ValueID::Blink:
  219. set_property_expanding_shorthands(style, CSS::PropertyID::TextDecorationLine, value, document);
  220. default:
  221. break;
  222. }
  223. return;
  224. }
  225. if (property_id == CSS::PropertyID::Overflow) {
  226. style.set_property(CSS::PropertyID::OverflowX, value);
  227. style.set_property(CSS::PropertyID::OverflowY, value);
  228. return;
  229. }
  230. if (property_id == CSS::PropertyID::Border) {
  231. set_property_expanding_shorthands(style, CSS::PropertyID::BorderTop, value, document);
  232. set_property_expanding_shorthands(style, CSS::PropertyID::BorderRight, value, document);
  233. set_property_expanding_shorthands(style, CSS::PropertyID::BorderBottom, value, document);
  234. set_property_expanding_shorthands(style, CSS::PropertyID::BorderLeft, value, document);
  235. return;
  236. }
  237. if (property_id == CSS::PropertyID::BorderTop
  238. || property_id == CSS::PropertyID::BorderRight
  239. || property_id == CSS::PropertyID::BorderBottom
  240. || property_id == CSS::PropertyID::BorderLeft) {
  241. Edge edge = Edge::All;
  242. switch (property_id) {
  243. case CSS::PropertyID::BorderTop:
  244. edge = Edge::Top;
  245. break;
  246. case CSS::PropertyID::BorderRight:
  247. edge = Edge::Right;
  248. break;
  249. case CSS::PropertyID::BorderBottom:
  250. edge = Edge::Bottom;
  251. break;
  252. case CSS::PropertyID::BorderLeft:
  253. edge = Edge::Left;
  254. break;
  255. default:
  256. break;
  257. }
  258. auto parts = split_on_whitespace(value.to_string());
  259. if (value.is_length()) {
  260. set_property_border_width(style, value, edge);
  261. return;
  262. }
  263. if (value.is_color()) {
  264. set_property_border_color(style, value, edge);
  265. return;
  266. }
  267. if (value.is_string()) {
  268. auto parts = split_on_whitespace(value.to_string());
  269. if (parts.size() == 1) {
  270. if (auto value = parse_line_style(context, parts[0])) {
  271. set_property_border_style(style, value.release_nonnull(), edge);
  272. set_property_border_color(style, ColorStyleValue::create(Gfx::Color::Black), edge);
  273. set_property_border_width(style, LengthStyleValue::create(Length(3, Length::Type::Px)), edge);
  274. return;
  275. }
  276. }
  277. RefPtr<LengthStyleValue> line_width_value;
  278. RefPtr<ColorStyleValue> color_value;
  279. RefPtr<StringStyleValue> line_style_value;
  280. for (auto& part : parts) {
  281. if (auto value = parse_line_width(context, part)) {
  282. if (line_width_value)
  283. return;
  284. line_width_value = move(value);
  285. continue;
  286. }
  287. if (auto value = parse_color(context, part)) {
  288. if (color_value)
  289. return;
  290. color_value = move(value);
  291. continue;
  292. }
  293. if (auto value = parse_line_style(context, part)) {
  294. if (line_style_value)
  295. return;
  296. line_style_value = move(value);
  297. continue;
  298. }
  299. }
  300. if (line_width_value)
  301. set_property_border_width(style, line_width_value.release_nonnull(), edge);
  302. if (color_value)
  303. set_property_border_color(style, color_value.release_nonnull(), edge);
  304. if (line_style_value)
  305. set_property_border_style(style, line_style_value.release_nonnull(), edge);
  306. return;
  307. }
  308. return;
  309. }
  310. if (property_id == CSS::PropertyID::BorderStyle) {
  311. auto parts = split_on_whitespace(value.to_string());
  312. if (value.is_string() && parts.size() == 3) {
  313. auto top = parse_css_value(context, parts[0]);
  314. auto right = parse_css_value(context, parts[1]);
  315. auto bottom = parse_css_value(context, parts[2]);
  316. auto left = parse_css_value(context, parts[1]);
  317. if (top && right && bottom && left) {
  318. style.set_property(CSS::PropertyID::BorderTopStyle, *top);
  319. style.set_property(CSS::PropertyID::BorderRightStyle, *right);
  320. style.set_property(CSS::PropertyID::BorderBottomStyle, *bottom);
  321. style.set_property(CSS::PropertyID::BorderLeftStyle, *left);
  322. }
  323. } else {
  324. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  325. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  326. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  327. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  328. }
  329. return;
  330. }
  331. if (property_id == CSS::PropertyID::BorderWidth) {
  332. auto parts = split_on_whitespace(value.to_string());
  333. if (value.is_string() && parts.size() == 2) {
  334. auto vertical_border_width = parse_css_value(context, parts[0]);
  335. auto horizontal_border_width = parse_css_value(context, parts[1]);
  336. if (vertical_border_width && horizontal_border_width) {
  337. style.set_property(CSS::PropertyID::BorderTopWidth, *vertical_border_width);
  338. style.set_property(CSS::PropertyID::BorderRightWidth, *horizontal_border_width);
  339. style.set_property(CSS::PropertyID::BorderBottomWidth, *vertical_border_width);
  340. style.set_property(CSS::PropertyID::BorderLeftWidth, *horizontal_border_width);
  341. }
  342. } else {
  343. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  344. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  345. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  346. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  347. }
  348. return;
  349. }
  350. if (property_id == CSS::PropertyID::BorderColor) {
  351. auto parts = split_on_whitespace(value.to_string());
  352. if (value.is_string() && parts.size() == 4) {
  353. auto top = parse_css_value(context, parts[0]);
  354. auto right = parse_css_value(context, parts[1]);
  355. auto bottom = parse_css_value(context, parts[2]);
  356. auto left = parse_css_value(context, parts[3]);
  357. if (top && right && bottom && left) {
  358. style.set_property(CSS::PropertyID::BorderTopColor, *top);
  359. style.set_property(CSS::PropertyID::BorderRightColor, *right);
  360. style.set_property(CSS::PropertyID::BorderBottomColor, *bottom);
  361. style.set_property(CSS::PropertyID::BorderLeftColor, *left);
  362. }
  363. } else {
  364. style.set_property(CSS::PropertyID::BorderTopColor, value);
  365. style.set_property(CSS::PropertyID::BorderRightColor, value);
  366. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  367. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  368. }
  369. return;
  370. }
  371. if (property_id == CSS::PropertyID::Background) {
  372. if (value.is_identifier() && static_cast<const IdentifierStyleValue&>(value).id() == CSS::ValueID::None) {
  373. style.set_property(CSS::PropertyID::BackgroundColor, ColorStyleValue::create(Color::Transparent));
  374. return;
  375. }
  376. auto parts = split_on_whitespace(value.to_string());
  377. NonnullRefPtrVector<StyleValue> values;
  378. for (auto& part : parts) {
  379. auto value = parse_css_value(context, part);
  380. if (!value)
  381. return;
  382. values.append(value.release_nonnull());
  383. }
  384. // HACK: Disallow more than one color value in a 'background' shorthand
  385. size_t color_value_count = 0;
  386. for (auto& value : values)
  387. color_value_count += value.is_color();
  388. if (values[0].is_color() && color_value_count == 1)
  389. style.set_property(CSS::PropertyID::BackgroundColor, values[0]);
  390. for (auto& value : values) {
  391. if (!value.is_string())
  392. continue;
  393. auto string = value.to_string();
  394. set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, value, document);
  395. }
  396. return;
  397. }
  398. if (property_id == CSS::PropertyID::BackgroundImage) {
  399. if (!value.is_string())
  400. return;
  401. auto string = value.to_string();
  402. if (!string.starts_with("url("))
  403. return;
  404. if (!string.ends_with(')'))
  405. return;
  406. auto url = string.substring_view(4, string.length() - 5);
  407. if (url.length() >= 2 && url.starts_with('"') && url.ends_with('"'))
  408. url = url.substring_view(1, url.length() - 2);
  409. else if (url.length() >= 2 && url.starts_with('\'') && url.ends_with('\''))
  410. url = url.substring_view(1, url.length() - 2);
  411. auto background_image_value = ImageStyleValue::create(document.complete_url(url), document);
  412. style.set_property(CSS::PropertyID::BackgroundImage, move(background_image_value));
  413. return;
  414. }
  415. if (property_id == CSS::PropertyID::Margin) {
  416. if (value.is_length()) {
  417. style.set_property(CSS::PropertyID::MarginTop, value);
  418. style.set_property(CSS::PropertyID::MarginRight, value);
  419. style.set_property(CSS::PropertyID::MarginBottom, value);
  420. style.set_property(CSS::PropertyID::MarginLeft, value);
  421. return;
  422. }
  423. if (value.is_string()) {
  424. auto parts = split_on_whitespace(value.to_string());
  425. if (value.is_string() && parts.size() == 2) {
  426. auto vertical = parse_css_value(context, parts[0]);
  427. auto horizontal = parse_css_value(context, parts[1]);
  428. if (vertical && horizontal) {
  429. style.set_property(CSS::PropertyID::MarginTop, *vertical);
  430. style.set_property(CSS::PropertyID::MarginBottom, *vertical);
  431. style.set_property(CSS::PropertyID::MarginLeft, *horizontal);
  432. style.set_property(CSS::PropertyID::MarginRight, *horizontal);
  433. }
  434. return;
  435. }
  436. if (value.is_string() && parts.size() == 3) {
  437. auto top = parse_css_value(context, parts[0]);
  438. auto horizontal = parse_css_value(context, parts[1]);
  439. auto bottom = parse_css_value(context, parts[2]);
  440. if (top && horizontal && bottom) {
  441. style.set_property(CSS::PropertyID::MarginTop, *top);
  442. style.set_property(CSS::PropertyID::MarginBottom, *bottom);
  443. style.set_property(CSS::PropertyID::MarginLeft, *horizontal);
  444. style.set_property(CSS::PropertyID::MarginRight, *horizontal);
  445. }
  446. return;
  447. }
  448. if (value.is_string() && parts.size() == 4) {
  449. auto top = parse_css_value(context, parts[0]);
  450. auto right = parse_css_value(context, parts[1]);
  451. auto bottom = parse_css_value(context, parts[2]);
  452. auto left = parse_css_value(context, parts[3]);
  453. if (top && right && bottom && left) {
  454. style.set_property(CSS::PropertyID::MarginTop, *top);
  455. style.set_property(CSS::PropertyID::MarginBottom, *bottom);
  456. style.set_property(CSS::PropertyID::MarginLeft, *left);
  457. style.set_property(CSS::PropertyID::MarginRight, *right);
  458. }
  459. return;
  460. }
  461. dbgln("Unsure what to do with CSS margin value '{}'", value.to_string());
  462. return;
  463. }
  464. return;
  465. }
  466. if (property_id == CSS::PropertyID::Padding) {
  467. if (value.is_length()) {
  468. style.set_property(CSS::PropertyID::PaddingTop, value);
  469. style.set_property(CSS::PropertyID::PaddingRight, value);
  470. style.set_property(CSS::PropertyID::PaddingBottom, value);
  471. style.set_property(CSS::PropertyID::PaddingLeft, value);
  472. return;
  473. }
  474. if (value.is_string()) {
  475. auto parts = split_on_whitespace(value.to_string());
  476. if (value.is_string() && parts.size() == 2) {
  477. auto vertical = parse_css_value(context, parts[0]);
  478. auto horizontal = parse_css_value(context, parts[1]);
  479. if (vertical && horizontal) {
  480. style.set_property(CSS::PropertyID::PaddingTop, *vertical);
  481. style.set_property(CSS::PropertyID::PaddingBottom, *vertical);
  482. style.set_property(CSS::PropertyID::PaddingLeft, *horizontal);
  483. style.set_property(CSS::PropertyID::PaddingRight, *horizontal);
  484. }
  485. return;
  486. }
  487. if (value.is_string() && parts.size() == 3) {
  488. auto top = parse_css_value(context, parts[0]);
  489. auto horizontal = parse_css_value(context, parts[1]);
  490. auto bottom = parse_css_value(context, parts[2]);
  491. if (top && bottom && horizontal) {
  492. style.set_property(CSS::PropertyID::PaddingTop, *top);
  493. style.set_property(CSS::PropertyID::PaddingBottom, *bottom);
  494. style.set_property(CSS::PropertyID::PaddingLeft, *horizontal);
  495. style.set_property(CSS::PropertyID::PaddingRight, *horizontal);
  496. }
  497. return;
  498. }
  499. if (value.is_string() && parts.size() == 4) {
  500. auto top = parse_css_value(context, parts[0]);
  501. auto right = parse_css_value(context, parts[1]);
  502. auto bottom = parse_css_value(context, parts[2]);
  503. auto left = parse_css_value(context, parts[3]);
  504. if (top && bottom && left && right) {
  505. style.set_property(CSS::PropertyID::PaddingTop, *top);
  506. style.set_property(CSS::PropertyID::PaddingBottom, *bottom);
  507. style.set_property(CSS::PropertyID::PaddingLeft, *left);
  508. style.set_property(CSS::PropertyID::PaddingRight, *right);
  509. }
  510. return;
  511. }
  512. dbgln("Unsure what to do with CSS padding value '{}'", value.to_string());
  513. return;
  514. }
  515. return;
  516. }
  517. if (property_id == CSS::PropertyID::ListStyle) {
  518. auto parts = split_on_whitespace(value.to_string());
  519. if (!parts.is_empty()) {
  520. auto value = parse_css_value(context, parts[0]);
  521. if (!value)
  522. return;
  523. style.set_property(CSS::PropertyID::ListStyleType, value.release_nonnull());
  524. }
  525. return;
  526. }
  527. style.set_property(property_id, value);
  528. }
  529. NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(const DOM::Element& element) const
  530. {
  531. auto style = StyleProperties::create();
  532. if (auto* parent_style = element.parent_element() ? element.parent_element()->specified_css_values() : nullptr) {
  533. parent_style->for_each_property([&](auto property_id, auto& value) {
  534. if (is_inherited_property(property_id))
  535. set_property_expanding_shorthands(style, property_id, value, m_document);
  536. });
  537. }
  538. element.apply_presentational_hints(*style);
  539. auto matching_rules = collect_matching_rules(element);
  540. sort_matching_rules(matching_rules);
  541. for (auto& match : matching_rules) {
  542. for (auto& property : match.rule->declaration().properties()) {
  543. set_property_expanding_shorthands(style, property.property_id, property.value, m_document);
  544. }
  545. }
  546. if (auto* inline_style = element.inline_style()) {
  547. for (auto& property : inline_style->properties()) {
  548. set_property_expanding_shorthands(style, property.property_id, property.value, m_document);
  549. }
  550. }
  551. return style;
  552. }
  553. }