StyleResolver.cpp 21 KB

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