StyleResolver.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 <LibHTML/CSS/SelectorEngine.h>
  27. #include <LibHTML/CSS/StyleResolver.h>
  28. #include <LibHTML/CSS/StyleSheet.h>
  29. #include <LibHTML/DOM/Document.h>
  30. #include <LibHTML/DOM/Element.h>
  31. #include <LibHTML/Dump.h>
  32. #include <LibHTML/Parser/CSSParser.h>
  33. #include <ctype.h>
  34. #include <stdio.h>
  35. StyleResolver::StyleResolver(Document& document)
  36. : m_document(document)
  37. {
  38. }
  39. StyleResolver::~StyleResolver()
  40. {
  41. }
  42. static StyleSheet& default_stylesheet()
  43. {
  44. static StyleSheet* sheet;
  45. if (!sheet) {
  46. extern const char default_stylesheet_source[];
  47. String css = default_stylesheet_source;
  48. sheet = parse_css(css).leak_ref();
  49. }
  50. return *sheet;
  51. }
  52. template<typename Callback>
  53. void StyleResolver::for_each_stylesheet(Callback callback) const
  54. {
  55. callback(default_stylesheet());
  56. for (auto& sheet : document().stylesheets()) {
  57. callback(sheet);
  58. }
  59. }
  60. NonnullRefPtrVector<StyleRule> StyleResolver::collect_matching_rules(const Element& element) const
  61. {
  62. NonnullRefPtrVector<StyleRule> matching_rules;
  63. for_each_stylesheet([&](auto& sheet) {
  64. for (auto& rule : sheet.rules()) {
  65. for (auto& selector : rule.selectors()) {
  66. if (SelectorEngine::matches(selector, element)) {
  67. matching_rules.append(rule);
  68. break;
  69. }
  70. }
  71. }
  72. });
  73. #ifdef HTML_DEBUG
  74. dbgprintf("Rules matching Element{%p}\n", &element);
  75. for (auto& rule : matching_rules) {
  76. dump_rule(rule);
  77. }
  78. #endif
  79. return matching_rules;
  80. }
  81. bool StyleResolver::is_inherited_property(CSS::PropertyID property_id)
  82. {
  83. static HashTable<CSS::PropertyID> inherited_properties;
  84. if (inherited_properties.is_empty()) {
  85. inherited_properties.set(CSS::PropertyID::BorderCollapse);
  86. inherited_properties.set(CSS::PropertyID::BorderSpacing);
  87. inherited_properties.set(CSS::PropertyID::Color);
  88. inherited_properties.set(CSS::PropertyID::FontFamily);
  89. inherited_properties.set(CSS::PropertyID::FontSize);
  90. inherited_properties.set(CSS::PropertyID::FontStyle);
  91. inherited_properties.set(CSS::PropertyID::FontVariant);
  92. inherited_properties.set(CSS::PropertyID::FontWeight);
  93. inherited_properties.set(CSS::PropertyID::LetterSpacing);
  94. inherited_properties.set(CSS::PropertyID::LineHeight);
  95. inherited_properties.set(CSS::PropertyID::ListStyle);
  96. inherited_properties.set(CSS::PropertyID::ListStyleImage);
  97. inherited_properties.set(CSS::PropertyID::ListStylePosition);
  98. inherited_properties.set(CSS::PropertyID::ListStyleType);
  99. inherited_properties.set(CSS::PropertyID::TextAlign);
  100. inherited_properties.set(CSS::PropertyID::TextIndent);
  101. inherited_properties.set(CSS::PropertyID::TextTransform);
  102. inherited_properties.set(CSS::PropertyID::Visibility);
  103. inherited_properties.set(CSS::PropertyID::WhiteSpace);
  104. inherited_properties.set(CSS::PropertyID::WordSpacing);
  105. // FIXME: This property is not supposed to be inherited, but we currently
  106. // rely on inheritance to propagate decorations into line boxes.
  107. inherited_properties.set(CSS::PropertyID::TextDecoration);
  108. }
  109. return inherited_properties.contains(property_id);
  110. }
  111. static Vector<String> split_on_whitespace(const StringView& string)
  112. {
  113. if (string.is_empty())
  114. return {};
  115. Vector<String> v;
  116. size_t substart = 0;
  117. for (size_t i = 0; i < string.length(); ++i) {
  118. char ch = string.characters_without_null_termination()[i];
  119. if (isspace(ch)) {
  120. size_t sublen = i - substart;
  121. if (sublen != 0)
  122. v.append(string.substring_view(substart, sublen));
  123. substart = i + 1;
  124. }
  125. }
  126. size_t taillen = string.length() - substart;
  127. if (taillen != 0)
  128. v.append(string.substring_view(substart, taillen));
  129. return v;
  130. }
  131. static void set_property_expanding_shorthands(StyleProperties& style, CSS::PropertyID property_id, const StyleValue& value)
  132. {
  133. if (property_id == CSS::PropertyID::BorderStyle) {
  134. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  135. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  136. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  137. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  138. return;
  139. }
  140. if (property_id == CSS::PropertyID::BorderWidth) {
  141. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  142. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  143. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  144. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  145. return;
  146. }
  147. if (property_id == CSS::PropertyID::BorderColor) {
  148. style.set_property(CSS::PropertyID::BorderTopColor, value);
  149. style.set_property(CSS::PropertyID::BorderRightColor, value);
  150. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  151. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  152. return;
  153. }
  154. if (property_id == CSS::PropertyID::Margin) {
  155. if (value.is_length()) {
  156. style.set_property(CSS::PropertyID::MarginTop, value);
  157. style.set_property(CSS::PropertyID::MarginRight, value);
  158. style.set_property(CSS::PropertyID::MarginBottom, value);
  159. style.set_property(CSS::PropertyID::MarginLeft, value);
  160. return;
  161. }
  162. if (value.is_string()) {
  163. auto parts = split_on_whitespace(value.to_string());
  164. if (parts.size() == 2) {
  165. auto vertical = parse_css_value(parts[0]);
  166. auto horizontal = parse_css_value(parts[1]);
  167. style.set_property(CSS::PropertyID::MarginTop, vertical);
  168. style.set_property(CSS::PropertyID::MarginBottom, vertical);
  169. style.set_property(CSS::PropertyID::MarginLeft, horizontal);
  170. style.set_property(CSS::PropertyID::MarginRight, horizontal);
  171. return;
  172. }
  173. if (parts.size() == 3) {
  174. auto top = parse_css_value(parts[0]);
  175. auto horizontal = parse_css_value(parts[1]);
  176. auto bottom = parse_css_value(parts[2]);
  177. style.set_property(CSS::PropertyID::MarginTop, top);
  178. style.set_property(CSS::PropertyID::MarginBottom, bottom);
  179. style.set_property(CSS::PropertyID::MarginLeft, horizontal);
  180. style.set_property(CSS::PropertyID::MarginRight, horizontal);
  181. return;
  182. }
  183. if (parts.size() == 4) {
  184. auto top = parse_css_value(parts[0]);
  185. auto right = parse_css_value(parts[1]);
  186. auto bottom = parse_css_value(parts[2]);
  187. auto left = parse_css_value(parts[3]);
  188. style.set_property(CSS::PropertyID::MarginTop, top);
  189. style.set_property(CSS::PropertyID::MarginBottom, bottom);
  190. style.set_property(CSS::PropertyID::MarginLeft, left);
  191. style.set_property(CSS::PropertyID::MarginRight, right);
  192. return;
  193. }
  194. dbg() << "Unsure what to do with CSS margin value '" << value.to_string() << "'";
  195. return;
  196. }
  197. return;
  198. }
  199. if (property_id == CSS::PropertyID::Padding) {
  200. if (value.is_length()) {
  201. style.set_property(CSS::PropertyID::PaddingTop, value);
  202. style.set_property(CSS::PropertyID::PaddingRight, value);
  203. style.set_property(CSS::PropertyID::PaddingBottom, value);
  204. style.set_property(CSS::PropertyID::PaddingLeft, value);
  205. return;
  206. }
  207. if (value.is_string()) {
  208. auto parts = split_on_whitespace(value.to_string());
  209. if (parts.size() == 2) {
  210. auto vertical = parse_css_value(parts[0]);
  211. auto horizontal = parse_css_value(parts[1]);
  212. style.set_property(CSS::PropertyID::PaddingTop, vertical);
  213. style.set_property(CSS::PropertyID::PaddingBottom, vertical);
  214. style.set_property(CSS::PropertyID::PaddingLeft, horizontal);
  215. style.set_property(CSS::PropertyID::PaddingRight, horizontal);
  216. return;
  217. }
  218. if (parts.size() == 3) {
  219. auto top = parse_css_value(parts[0]);
  220. auto horizontal = parse_css_value(parts[1]);
  221. auto bottom = parse_css_value(parts[2]);
  222. style.set_property(CSS::PropertyID::PaddingTop, top);
  223. style.set_property(CSS::PropertyID::PaddingBottom, bottom);
  224. style.set_property(CSS::PropertyID::PaddingLeft, horizontal);
  225. style.set_property(CSS::PropertyID::PaddingRight, horizontal);
  226. return;
  227. }
  228. if (parts.size() == 4) {
  229. auto top = parse_css_value(parts[0]);
  230. auto right = parse_css_value(parts[1]);
  231. auto bottom = parse_css_value(parts[2]);
  232. auto left = parse_css_value(parts[3]);
  233. style.set_property(CSS::PropertyID::PaddingTop, top);
  234. style.set_property(CSS::PropertyID::PaddingBottom, bottom);
  235. style.set_property(CSS::PropertyID::PaddingLeft, left);
  236. style.set_property(CSS::PropertyID::PaddingRight, right);
  237. return;
  238. }
  239. dbg() << "Unsure what to do with CSS padding value '" << value.to_string() << "'";
  240. return;
  241. }
  242. return;
  243. }
  244. style.set_property(property_id, value);
  245. }
  246. NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(const Element& element, const StyleProperties* parent_style) const
  247. {
  248. auto style = StyleProperties::create();
  249. if (parent_style) {
  250. parent_style->for_each_property([&](auto property_id, auto& value) {
  251. if (is_inherited_property(property_id))
  252. set_property_expanding_shorthands(style, property_id, value);
  253. });
  254. }
  255. element.apply_presentational_hints(*style);
  256. auto matching_rules = collect_matching_rules(element);
  257. for (auto& rule : matching_rules) {
  258. for (auto& property : rule.declaration().properties()) {
  259. set_property_expanding_shorthands(style, property.property_id, property.value);
  260. }
  261. }
  262. auto style_attribute = element.attribute("style");
  263. if (!style_attribute.is_null()) {
  264. if (auto declaration = parse_css_declaration(style_attribute)) {
  265. for (auto& property : declaration->properties()) {
  266. set_property_expanding_shorthands(style, property.property_id, property.value);
  267. }
  268. }
  269. }
  270. return style;
  271. }