StyleResolver.cpp 24 KB

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