StyleResolver.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. #ifdef HTML_DEBUG
  94. dbgprintf("Rules matching Element{%p}\n", &element);
  95. for (auto& rule : matching_rules) {
  96. dump_rule(rule);
  97. }
  98. #endif
  99. return matching_rules;
  100. }
  101. bool StyleResolver::is_inherited_property(CSS::PropertyID property_id)
  102. {
  103. static HashTable<CSS::PropertyID> inherited_properties;
  104. if (inherited_properties.is_empty()) {
  105. inherited_properties.set(CSS::PropertyID::BorderCollapse);
  106. inherited_properties.set(CSS::PropertyID::BorderSpacing);
  107. inherited_properties.set(CSS::PropertyID::Color);
  108. inherited_properties.set(CSS::PropertyID::FontFamily);
  109. inherited_properties.set(CSS::PropertyID::FontSize);
  110. inherited_properties.set(CSS::PropertyID::FontStyle);
  111. inherited_properties.set(CSS::PropertyID::FontVariant);
  112. inherited_properties.set(CSS::PropertyID::FontWeight);
  113. inherited_properties.set(CSS::PropertyID::LetterSpacing);
  114. inherited_properties.set(CSS::PropertyID::LineHeight);
  115. inherited_properties.set(CSS::PropertyID::ListStyle);
  116. inherited_properties.set(CSS::PropertyID::ListStyleImage);
  117. inherited_properties.set(CSS::PropertyID::ListStylePosition);
  118. inherited_properties.set(CSS::PropertyID::ListStyleType);
  119. inherited_properties.set(CSS::PropertyID::TextAlign);
  120. inherited_properties.set(CSS::PropertyID::TextIndent);
  121. inherited_properties.set(CSS::PropertyID::TextTransform);
  122. inherited_properties.set(CSS::PropertyID::Visibility);
  123. inherited_properties.set(CSS::PropertyID::WhiteSpace);
  124. inherited_properties.set(CSS::PropertyID::WordSpacing);
  125. // FIXME: This property is not supposed to be inherited, but we currently
  126. // rely on inheritance to propagate decorations into line boxes.
  127. inherited_properties.set(CSS::PropertyID::TextDecoration);
  128. }
  129. return inherited_properties.contains(property_id);
  130. }
  131. static Vector<String> split_on_whitespace(const StringView& string)
  132. {
  133. if (string.is_empty())
  134. return {};
  135. Vector<String> v;
  136. size_t substart = 0;
  137. for (size_t i = 0; i < string.length(); ++i) {
  138. char ch = string.characters_without_null_termination()[i];
  139. if (isspace(ch)) {
  140. size_t sublen = i - substart;
  141. if (sublen != 0)
  142. v.append(string.substring_view(substart, sublen));
  143. substart = i + 1;
  144. }
  145. }
  146. size_t taillen = string.length() - substart;
  147. if (taillen != 0)
  148. v.append(string.substring_view(substart, taillen));
  149. return v;
  150. }
  151. enum class Edge {
  152. Top,
  153. Right,
  154. Bottom,
  155. Left,
  156. All,
  157. };
  158. static bool contains(Edge a, Edge b)
  159. {
  160. return a == b || b == Edge::All;
  161. }
  162. static inline void set_property_border_width(StyleProperties& style, const StyleValue& value, Edge edge)
  163. {
  164. ASSERT(value.is_length());
  165. if (contains(Edge::Top, edge))
  166. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  167. if (contains(Edge::Right, edge))
  168. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  169. if (contains(Edge::Bottom, edge))
  170. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  171. if (contains(Edge::Left, edge))
  172. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  173. }
  174. static inline void set_property_border_color(StyleProperties& style, const StyleValue& value, Edge edge)
  175. {
  176. ASSERT(value.is_color());
  177. if (contains(Edge::Top, edge))
  178. style.set_property(CSS::PropertyID::BorderTopColor, value);
  179. if (contains(Edge::Right, edge))
  180. style.set_property(CSS::PropertyID::BorderRightColor, value);
  181. if (contains(Edge::Bottom, edge))
  182. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  183. if (contains(Edge::Left, edge))
  184. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  185. }
  186. static inline void set_property_border_style(StyleProperties& style, const StyleValue& value, Edge edge)
  187. {
  188. ASSERT(value.is_string());
  189. if (contains(Edge::Top, edge))
  190. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  191. if (contains(Edge::Right, edge))
  192. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  193. if (contains(Edge::Bottom, edge))
  194. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  195. if (contains(Edge::Left, edge))
  196. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  197. }
  198. static void set_property_expanding_shorthands(StyleProperties& style, CSS::PropertyID property_id, const StyleValue& value, DOM::Document& document)
  199. {
  200. CSS::ParsingContext context(document);
  201. if (property_id == CSS::PropertyID::Border) {
  202. set_property_expanding_shorthands(style, CSS::PropertyID::BorderTop, value, document);
  203. set_property_expanding_shorthands(style, CSS::PropertyID::BorderRight, value, document);
  204. set_property_expanding_shorthands(style, CSS::PropertyID::BorderBottom, value, document);
  205. set_property_expanding_shorthands(style, CSS::PropertyID::BorderLeft, value, document);
  206. }
  207. if (property_id == CSS::PropertyID::BorderTop
  208. || property_id == CSS::PropertyID::BorderRight
  209. || property_id == CSS::PropertyID::BorderBottom
  210. || property_id == CSS::PropertyID::BorderLeft) {
  211. Edge edge = Edge::All;
  212. switch (property_id) {
  213. case CSS::PropertyID::BorderTop:
  214. edge = Edge::Top;
  215. break;
  216. case CSS::PropertyID::BorderRight:
  217. edge = Edge::Right;
  218. break;
  219. case CSS::PropertyID::BorderBottom:
  220. edge = Edge::Bottom;
  221. break;
  222. case CSS::PropertyID::BorderLeft:
  223. edge = Edge::Left;
  224. break;
  225. default:
  226. break;
  227. }
  228. auto parts = split_on_whitespace(value.to_string());
  229. if (value.is_length()) {
  230. set_property_border_width(style, value, edge);
  231. return;
  232. }
  233. if (value.is_color()) {
  234. set_property_border_color(style, value, edge);
  235. return;
  236. }
  237. if (value.is_string()) {
  238. auto parts = split_on_whitespace(value.to_string());
  239. if (parts.size() == 1) {
  240. if (auto value = parse_line_style(context, parts[0])) {
  241. set_property_border_style(style, value.release_nonnull(), edge);
  242. set_property_border_color(style, ColorStyleValue::create(Gfx::Color::Black), edge);
  243. set_property_border_width(style, LengthStyleValue::create(Length(3, Length::Type::Px)), edge);
  244. return;
  245. }
  246. }
  247. RefPtr<LengthStyleValue> line_width_value;
  248. RefPtr<ColorStyleValue> color_value;
  249. RefPtr<StringStyleValue> line_style_value;
  250. for (auto& part : parts) {
  251. if (auto value = parse_line_width(context, part)) {
  252. if (line_width_value)
  253. return;
  254. line_width_value = move(value);
  255. continue;
  256. }
  257. if (auto value = parse_color(context, part)) {
  258. if (color_value)
  259. return;
  260. color_value = move(value);
  261. continue;
  262. }
  263. if (auto value = parse_line_style(context, part)) {
  264. if (line_style_value)
  265. return;
  266. line_style_value = move(value);
  267. continue;
  268. }
  269. }
  270. if (line_width_value)
  271. set_property_border_width(style, line_width_value.release_nonnull(), edge);
  272. if (color_value)
  273. set_property_border_color(style, color_value.release_nonnull(), edge);
  274. if (line_style_value)
  275. set_property_border_style(style, line_style_value.release_nonnull(), edge);
  276. return;
  277. }
  278. return;
  279. }
  280. if (property_id == CSS::PropertyID::BorderStyle) {
  281. auto parts = split_on_whitespace(value.to_string());
  282. if (value.is_string() && parts.size() == 3) {
  283. auto top = parse_css_value(context, parts[0]);
  284. auto right = parse_css_value(context, parts[1]);
  285. auto bottom = parse_css_value(context, parts[2]);
  286. auto left = parse_css_value(context, parts[1]);
  287. if (top && right && bottom && left) {
  288. style.set_property(CSS::PropertyID::BorderTopStyle, *top);
  289. style.set_property(CSS::PropertyID::BorderRightStyle, *right);
  290. style.set_property(CSS::PropertyID::BorderBottomStyle, *bottom);
  291. style.set_property(CSS::PropertyID::BorderLeftStyle, *left);
  292. }
  293. } else {
  294. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  295. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  296. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  297. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  298. }
  299. return;
  300. }
  301. if (property_id == CSS::PropertyID::BorderWidth) {
  302. auto parts = split_on_whitespace(value.to_string());
  303. if (value.is_string() && parts.size() == 2) {
  304. auto vertical_border_width = parse_css_value(context, parts[0]);
  305. auto horizontal_border_width = parse_css_value(context, parts[1]);
  306. if (vertical_border_width && horizontal_border_width) {
  307. style.set_property(CSS::PropertyID::BorderTopWidth, *vertical_border_width);
  308. style.set_property(CSS::PropertyID::BorderRightWidth, *horizontal_border_width);
  309. style.set_property(CSS::PropertyID::BorderBottomWidth, *vertical_border_width);
  310. style.set_property(CSS::PropertyID::BorderLeftWidth, *horizontal_border_width);
  311. }
  312. } else {
  313. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  314. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  315. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  316. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  317. }
  318. return;
  319. }
  320. if (property_id == CSS::PropertyID::BorderColor) {
  321. auto parts = split_on_whitespace(value.to_string());
  322. if (value.is_string() && parts.size() == 4) {
  323. auto top = parse_css_value(context, parts[0]);
  324. auto right = parse_css_value(context, parts[1]);
  325. auto bottom = parse_css_value(context, parts[2]);
  326. auto left = parse_css_value(context, parts[3]);
  327. if (top && right && bottom && left) {
  328. style.set_property(CSS::PropertyID::BorderTopColor, *top);
  329. style.set_property(CSS::PropertyID::BorderRightColor, *right);
  330. style.set_property(CSS::PropertyID::BorderBottomColor, *bottom);
  331. style.set_property(CSS::PropertyID::BorderLeftColor, *left);
  332. }
  333. } else {
  334. style.set_property(CSS::PropertyID::BorderTopColor, value);
  335. style.set_property(CSS::PropertyID::BorderRightColor, value);
  336. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  337. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  338. }
  339. return;
  340. }
  341. if (property_id == CSS::PropertyID::Background) {
  342. if (value.to_string() == "none") {
  343. style.set_property(CSS::PropertyID::BackgroundColor, ColorStyleValue::create(Color::Transparent));
  344. return;
  345. }
  346. auto parts = split_on_whitespace(value.to_string());
  347. NonnullRefPtrVector<StyleValue> values;
  348. for (auto& part : parts) {
  349. auto value = parse_css_value(context, part);
  350. if (!value)
  351. return;
  352. values.append(value.release_nonnull());
  353. }
  354. // HACK: Disallow more than one color value in a 'background' shorthand
  355. size_t color_value_count = 0;
  356. for (auto& value : values)
  357. color_value_count += value.is_color();
  358. if (values[0].is_color() && color_value_count == 1)
  359. style.set_property(CSS::PropertyID::BackgroundColor, values[0]);
  360. for (auto& value : values) {
  361. if (!value.is_string())
  362. continue;
  363. auto string = value.to_string();
  364. if (!string.starts_with("url("))
  365. continue;
  366. if (!string.ends_with(')'))
  367. continue;
  368. auto url = string.substring_view(4, string.length() - 5);
  369. if (url.length() >= 2 && url.starts_with('"') && url.ends_with('"'))
  370. url = url.substring_view(1, url.length() - 2);
  371. else if (url.length() >= 2 && url.starts_with('\'') && url.ends_with('\''))
  372. url = url.substring_view(1, url.length() - 2);
  373. auto background_image_value = ImageStyleValue::create(document.complete_url(url), document);
  374. style.set_property(CSS::PropertyID::BackgroundImage, move(background_image_value));
  375. }
  376. return;
  377. }
  378. if (property_id == CSS::PropertyID::Margin) {
  379. if (value.is_length()) {
  380. style.set_property(CSS::PropertyID::MarginTop, value);
  381. style.set_property(CSS::PropertyID::MarginRight, value);
  382. style.set_property(CSS::PropertyID::MarginBottom, value);
  383. style.set_property(CSS::PropertyID::MarginLeft, value);
  384. return;
  385. }
  386. if (value.is_string()) {
  387. auto parts = split_on_whitespace(value.to_string());
  388. if (value.is_string() && parts.size() == 2) {
  389. auto vertical = parse_css_value(context, parts[0]);
  390. auto horizontal = parse_css_value(context, parts[1]);
  391. if (vertical && horizontal) {
  392. style.set_property(CSS::PropertyID::MarginTop, *vertical);
  393. style.set_property(CSS::PropertyID::MarginBottom, *vertical);
  394. style.set_property(CSS::PropertyID::MarginLeft, *horizontal);
  395. style.set_property(CSS::PropertyID::MarginRight, *horizontal);
  396. }
  397. return;
  398. }
  399. if (value.is_string() && parts.size() == 3) {
  400. auto top = parse_css_value(context, parts[0]);
  401. auto horizontal = parse_css_value(context, parts[1]);
  402. auto bottom = parse_css_value(context, parts[2]);
  403. if (top && horizontal && bottom) {
  404. style.set_property(CSS::PropertyID::MarginTop, *top);
  405. style.set_property(CSS::PropertyID::MarginBottom, *bottom);
  406. style.set_property(CSS::PropertyID::MarginLeft, *horizontal);
  407. style.set_property(CSS::PropertyID::MarginRight, *horizontal);
  408. }
  409. return;
  410. }
  411. if (value.is_string() && parts.size() == 4) {
  412. auto top = parse_css_value(context, parts[0]);
  413. auto right = parse_css_value(context, parts[1]);
  414. auto bottom = parse_css_value(context, parts[2]);
  415. auto left = parse_css_value(context, parts[3]);
  416. if (top && right && bottom && left) {
  417. style.set_property(CSS::PropertyID::MarginTop, *top);
  418. style.set_property(CSS::PropertyID::MarginBottom, *bottom);
  419. style.set_property(CSS::PropertyID::MarginLeft, *left);
  420. style.set_property(CSS::PropertyID::MarginRight, *right);
  421. }
  422. return;
  423. }
  424. dbg() << "Unsure what to do with CSS margin value '" << value.to_string() << "'";
  425. return;
  426. }
  427. return;
  428. }
  429. if (property_id == CSS::PropertyID::Padding) {
  430. if (value.is_length()) {
  431. style.set_property(CSS::PropertyID::PaddingTop, value);
  432. style.set_property(CSS::PropertyID::PaddingRight, value);
  433. style.set_property(CSS::PropertyID::PaddingBottom, value);
  434. style.set_property(CSS::PropertyID::PaddingLeft, value);
  435. return;
  436. }
  437. if (value.is_string()) {
  438. auto parts = split_on_whitespace(value.to_string());
  439. if (value.is_string() && parts.size() == 2) {
  440. auto vertical = parse_css_value(context, parts[0]);
  441. auto horizontal = parse_css_value(context, parts[1]);
  442. if (vertical && horizontal) {
  443. style.set_property(CSS::PropertyID::PaddingTop, *vertical);
  444. style.set_property(CSS::PropertyID::PaddingBottom, *vertical);
  445. style.set_property(CSS::PropertyID::PaddingLeft, *horizontal);
  446. style.set_property(CSS::PropertyID::PaddingRight, *horizontal);
  447. }
  448. return;
  449. }
  450. if (value.is_string() && parts.size() == 3) {
  451. auto top = parse_css_value(context, parts[0]);
  452. auto horizontal = parse_css_value(context, parts[1]);
  453. auto bottom = parse_css_value(context, parts[2]);
  454. if (top && bottom && horizontal) {
  455. style.set_property(CSS::PropertyID::PaddingTop, *top);
  456. style.set_property(CSS::PropertyID::PaddingBottom, *bottom);
  457. style.set_property(CSS::PropertyID::PaddingLeft, *horizontal);
  458. style.set_property(CSS::PropertyID::PaddingRight, *horizontal);
  459. }
  460. return;
  461. }
  462. if (value.is_string() && parts.size() == 4) {
  463. auto top = parse_css_value(context, parts[0]);
  464. auto right = parse_css_value(context, parts[1]);
  465. auto bottom = parse_css_value(context, parts[2]);
  466. auto left = parse_css_value(context, parts[3]);
  467. if (top && bottom && left && right) {
  468. style.set_property(CSS::PropertyID::PaddingTop, *top);
  469. style.set_property(CSS::PropertyID::PaddingBottom, *bottom);
  470. style.set_property(CSS::PropertyID::PaddingLeft, *left);
  471. style.set_property(CSS::PropertyID::PaddingRight, *right);
  472. }
  473. return;
  474. }
  475. dbg() << "Unsure what to do with CSS padding value '" << value.to_string() << "'";
  476. return;
  477. }
  478. return;
  479. }
  480. if (property_id == CSS::PropertyID::ListStyle) {
  481. auto parts = split_on_whitespace(value.to_string());
  482. if (!parts.is_empty()) {
  483. auto value = parse_css_value(context, parts[0]);
  484. if (!value)
  485. return;
  486. style.set_property(CSS::PropertyID::ListStyleType, value.release_nonnull());
  487. }
  488. return;
  489. }
  490. style.set_property(property_id, value);
  491. }
  492. NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(const DOM::Element& element, const StyleProperties* parent_style) const
  493. {
  494. auto style = StyleProperties::create();
  495. if (parent_style) {
  496. parent_style->for_each_property([&](auto property_id, auto& value) {
  497. if (is_inherited_property(property_id))
  498. set_property_expanding_shorthands(style, property_id, value, m_document);
  499. });
  500. }
  501. element.apply_presentational_hints(*style);
  502. auto matching_rules = collect_matching_rules(element);
  503. quick_sort(matching_rules, [&](MatchingRule& a, MatchingRule& b) {
  504. auto& a_selector = a.rule->selectors()[a.selector_index];
  505. auto& b_selector = b.rule->selectors()[b.selector_index];
  506. auto a_specificity = a_selector.specificity();
  507. auto b_specificity = b_selector.specificity();
  508. if (a_selector.specificity() == b_selector.specificity()) {
  509. if (a.style_sheet_index == b.style_sheet_index)
  510. return a.rule_index < b.rule_index;
  511. return a.style_sheet_index < b.style_sheet_index;
  512. }
  513. return a_specificity < b_specificity;
  514. });
  515. for (auto& match : matching_rules) {
  516. for (auto& property : match.rule->declaration().properties()) {
  517. set_property_expanding_shorthands(style, property.property_id, property.value, m_document);
  518. }
  519. }
  520. auto style_attribute = element.attribute(HTML::AttributeNames::style);
  521. if (!style_attribute.is_null()) {
  522. if (auto declaration = parse_css_declaration(CSS::ParsingContext(document()), style_attribute)) {
  523. for (auto& property : declaration->properties()) {
  524. set_property_expanding_shorthands(style, property.property_id, property.value, m_document);
  525. }
  526. }
  527. }
  528. return style;
  529. }
  530. }