StyleResolver.cpp 19 KB

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