StyleResolver.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 <LibWeb/CSS/SelectorEngine.h>
  27. #include <LibWeb/CSS/StyleResolver.h>
  28. #include <LibWeb/CSS/StyleSheet.h>
  29. #include <LibWeb/DOM/Document.h>
  30. #include <LibWeb/DOM/Element.h>
  31. #include <LibWeb/Dump.h>
  32. #include <LibWeb/Parser/CSSParser.h>
  33. #include <ctype.h>
  34. #include <stdio.h>
  35. namespace Web {
  36. StyleResolver::StyleResolver(Document& document)
  37. : m_document(document)
  38. {
  39. }
  40. StyleResolver::~StyleResolver()
  41. {
  42. }
  43. static StyleSheet& default_stylesheet()
  44. {
  45. static StyleSheet* sheet;
  46. if (!sheet) {
  47. extern const char default_stylesheet_source[];
  48. String css = default_stylesheet_source;
  49. sheet = parse_css(css).leak_ref();
  50. }
  51. return *sheet;
  52. }
  53. template<typename Callback>
  54. void StyleResolver::for_each_stylesheet(Callback callback) const
  55. {
  56. callback(default_stylesheet());
  57. for (auto& sheet : document().stylesheets()) {
  58. callback(sheet);
  59. }
  60. }
  61. NonnullRefPtrVector<StyleRule> StyleResolver::collect_matching_rules(const Element& element) const
  62. {
  63. NonnullRefPtrVector<StyleRule> matching_rules;
  64. for_each_stylesheet([&](auto& sheet) {
  65. for (auto& rule : sheet.rules()) {
  66. for (auto& selector : rule.selectors()) {
  67. if (SelectorEngine::matches(selector, element)) {
  68. matching_rules.append(rule);
  69. break;
  70. }
  71. }
  72. }
  73. });
  74. #ifdef HTML_DEBUG
  75. dbgprintf("Rules matching Element{%p}\n", &element);
  76. for (auto& rule : matching_rules) {
  77. dump_rule(rule);
  78. }
  79. #endif
  80. return matching_rules;
  81. }
  82. bool StyleResolver::is_inherited_property(CSS::PropertyID property_id)
  83. {
  84. static HashTable<CSS::PropertyID> inherited_properties;
  85. if (inherited_properties.is_empty()) {
  86. inherited_properties.set(CSS::PropertyID::BorderCollapse);
  87. inherited_properties.set(CSS::PropertyID::BorderSpacing);
  88. inherited_properties.set(CSS::PropertyID::Color);
  89. inherited_properties.set(CSS::PropertyID::FontFamily);
  90. inherited_properties.set(CSS::PropertyID::FontSize);
  91. inherited_properties.set(CSS::PropertyID::FontStyle);
  92. inherited_properties.set(CSS::PropertyID::FontVariant);
  93. inherited_properties.set(CSS::PropertyID::FontWeight);
  94. inherited_properties.set(CSS::PropertyID::LetterSpacing);
  95. inherited_properties.set(CSS::PropertyID::LineHeight);
  96. inherited_properties.set(CSS::PropertyID::ListStyle);
  97. inherited_properties.set(CSS::PropertyID::ListStyleImage);
  98. inherited_properties.set(CSS::PropertyID::ListStylePosition);
  99. inherited_properties.set(CSS::PropertyID::ListStyleType);
  100. inherited_properties.set(CSS::PropertyID::TextAlign);
  101. inherited_properties.set(CSS::PropertyID::TextIndent);
  102. inherited_properties.set(CSS::PropertyID::TextTransform);
  103. inherited_properties.set(CSS::PropertyID::Visibility);
  104. inherited_properties.set(CSS::PropertyID::WhiteSpace);
  105. inherited_properties.set(CSS::PropertyID::WordSpacing);
  106. // FIXME: This property is not supposed to be inherited, but we currently
  107. // rely on inheritance to propagate decorations into line boxes.
  108. inherited_properties.set(CSS::PropertyID::TextDecoration);
  109. }
  110. return inherited_properties.contains(property_id);
  111. }
  112. static Vector<String> split_on_whitespace(const StringView& string)
  113. {
  114. if (string.is_empty())
  115. return {};
  116. Vector<String> v;
  117. size_t substart = 0;
  118. for (size_t i = 0; i < string.length(); ++i) {
  119. char ch = string.characters_without_null_termination()[i];
  120. if (isspace(ch)) {
  121. size_t sublen = i - substart;
  122. if (sublen != 0)
  123. v.append(string.substring_view(substart, sublen));
  124. substart = i + 1;
  125. }
  126. }
  127. size_t taillen = string.length() - substart;
  128. if (taillen != 0)
  129. v.append(string.substring_view(substart, taillen));
  130. return v;
  131. }
  132. static inline void set_property_border_width(StyleProperties& style, const StyleValue& value)
  133. {
  134. ASSERT(value.is_length());
  135. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  136. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  137. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  138. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  139. }
  140. static inline void set_property_border_color(StyleProperties& style, const StyleValue& value)
  141. {
  142. ASSERT(value.is_color());
  143. style.set_property(CSS::PropertyID::BorderTopColor, value);
  144. style.set_property(CSS::PropertyID::BorderRightColor, value);
  145. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  146. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  147. }
  148. static inline void set_property_border_style(StyleProperties& style, const StyleValue& value)
  149. {
  150. ASSERT(value.is_string());
  151. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  152. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  153. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  154. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  155. }
  156. static void set_property_expanding_shorthands(StyleProperties& style, CSS::PropertyID property_id, const StyleValue& value)
  157. {
  158. if (property_id == CSS::PropertyID::Border) {
  159. auto parts = split_on_whitespace(value.to_string());
  160. if (value.is_length()) {
  161. set_property_border_width(style, value);
  162. return;
  163. }
  164. if (value.is_color()) {
  165. set_property_border_color(style, value);
  166. return;
  167. }
  168. if (value.is_string()) {
  169. auto parts = split_on_whitespace(value.to_string());
  170. if (parts.size() == 1) {
  171. if (auto value = parse_line_style(parts[0])) {
  172. set_property_border_style(style, value.release_nonnull());
  173. set_property_border_color(style, ColorStyleValue::create(Gfx::Color::Black));
  174. set_property_border_width(style, LengthStyleValue::create(Length(3, Length::Type::Absolute)));
  175. return;
  176. }
  177. }
  178. RefPtr<LengthStyleValue> line_width_value;
  179. RefPtr<ColorStyleValue> color_value;
  180. RefPtr<StringStyleValue> line_style_value;
  181. for (auto& part : parts) {
  182. if (auto value = parse_line_width(part)) {
  183. if (line_width_value)
  184. return;
  185. line_width_value = move(value);
  186. continue;
  187. }
  188. if (auto value = parse_color(part)) {
  189. if (color_value)
  190. return;
  191. color_value = move(value);
  192. continue;
  193. }
  194. if (auto value = parse_line_style(part)) {
  195. if (line_style_value)
  196. return;
  197. line_style_value = move(value);
  198. continue;
  199. }
  200. }
  201. if (line_width_value)
  202. set_property_border_width(style, line_width_value.release_nonnull());
  203. if (color_value)
  204. set_property_border_color(style, color_value.release_nonnull());
  205. if (line_style_value)
  206. set_property_border_style(style, line_style_value.release_nonnull());
  207. return;
  208. }
  209. return;
  210. }
  211. if (property_id == CSS::PropertyID::BorderStyle) {
  212. style.set_property(CSS::PropertyID::BorderTopStyle, value);
  213. style.set_property(CSS::PropertyID::BorderRightStyle, value);
  214. style.set_property(CSS::PropertyID::BorderBottomStyle, value);
  215. style.set_property(CSS::PropertyID::BorderLeftStyle, value);
  216. return;
  217. }
  218. if (property_id == CSS::PropertyID::BorderWidth) {
  219. style.set_property(CSS::PropertyID::BorderTopWidth, value);
  220. style.set_property(CSS::PropertyID::BorderRightWidth, value);
  221. style.set_property(CSS::PropertyID::BorderBottomWidth, value);
  222. style.set_property(CSS::PropertyID::BorderLeftWidth, value);
  223. return;
  224. }
  225. if (property_id == CSS::PropertyID::BorderColor) {
  226. style.set_property(CSS::PropertyID::BorderTopColor, value);
  227. style.set_property(CSS::PropertyID::BorderRightColor, value);
  228. style.set_property(CSS::PropertyID::BorderBottomColor, value);
  229. style.set_property(CSS::PropertyID::BorderLeftColor, value);
  230. return;
  231. }
  232. if (property_id == CSS::PropertyID::Background) {
  233. auto parts = split_on_whitespace(value.to_string());
  234. NonnullRefPtrVector<StyleValue> values;
  235. for (auto& part : parts) {
  236. values.append(parse_css_value(part));
  237. }
  238. if (values[0].is_color())
  239. style.set_property(CSS::PropertyID::BackgroundColor, values[0]);
  240. return;
  241. }
  242. if (property_id == CSS::PropertyID::Margin) {
  243. if (value.is_length()) {
  244. style.set_property(CSS::PropertyID::MarginTop, value);
  245. style.set_property(CSS::PropertyID::MarginRight, value);
  246. style.set_property(CSS::PropertyID::MarginBottom, value);
  247. style.set_property(CSS::PropertyID::MarginLeft, value);
  248. return;
  249. }
  250. if (value.is_string()) {
  251. auto parts = split_on_whitespace(value.to_string());
  252. if (parts.size() == 2) {
  253. auto vertical = parse_css_value(parts[0]);
  254. auto horizontal = parse_css_value(parts[1]);
  255. style.set_property(CSS::PropertyID::MarginTop, vertical);
  256. style.set_property(CSS::PropertyID::MarginBottom, vertical);
  257. style.set_property(CSS::PropertyID::MarginLeft, horizontal);
  258. style.set_property(CSS::PropertyID::MarginRight, horizontal);
  259. return;
  260. }
  261. if (parts.size() == 3) {
  262. auto top = parse_css_value(parts[0]);
  263. auto horizontal = parse_css_value(parts[1]);
  264. auto bottom = parse_css_value(parts[2]);
  265. style.set_property(CSS::PropertyID::MarginTop, top);
  266. style.set_property(CSS::PropertyID::MarginBottom, bottom);
  267. style.set_property(CSS::PropertyID::MarginLeft, horizontal);
  268. style.set_property(CSS::PropertyID::MarginRight, horizontal);
  269. return;
  270. }
  271. if (parts.size() == 4) {
  272. auto top = parse_css_value(parts[0]);
  273. auto right = parse_css_value(parts[1]);
  274. auto bottom = parse_css_value(parts[2]);
  275. auto left = parse_css_value(parts[3]);
  276. style.set_property(CSS::PropertyID::MarginTop, top);
  277. style.set_property(CSS::PropertyID::MarginBottom, bottom);
  278. style.set_property(CSS::PropertyID::MarginLeft, left);
  279. style.set_property(CSS::PropertyID::MarginRight, right);
  280. return;
  281. }
  282. dbg() << "Unsure what to do with CSS margin value '" << value.to_string() << "'";
  283. return;
  284. }
  285. return;
  286. }
  287. if (property_id == CSS::PropertyID::Padding) {
  288. if (value.is_length()) {
  289. style.set_property(CSS::PropertyID::PaddingTop, value);
  290. style.set_property(CSS::PropertyID::PaddingRight, value);
  291. style.set_property(CSS::PropertyID::PaddingBottom, value);
  292. style.set_property(CSS::PropertyID::PaddingLeft, value);
  293. return;
  294. }
  295. if (value.is_string()) {
  296. auto parts = split_on_whitespace(value.to_string());
  297. if (parts.size() == 2) {
  298. auto vertical = parse_css_value(parts[0]);
  299. auto horizontal = parse_css_value(parts[1]);
  300. style.set_property(CSS::PropertyID::PaddingTop, vertical);
  301. style.set_property(CSS::PropertyID::PaddingBottom, vertical);
  302. style.set_property(CSS::PropertyID::PaddingLeft, horizontal);
  303. style.set_property(CSS::PropertyID::PaddingRight, horizontal);
  304. return;
  305. }
  306. if (parts.size() == 3) {
  307. auto top = parse_css_value(parts[0]);
  308. auto horizontal = parse_css_value(parts[1]);
  309. auto bottom = parse_css_value(parts[2]);
  310. style.set_property(CSS::PropertyID::PaddingTop, top);
  311. style.set_property(CSS::PropertyID::PaddingBottom, bottom);
  312. style.set_property(CSS::PropertyID::PaddingLeft, horizontal);
  313. style.set_property(CSS::PropertyID::PaddingRight, horizontal);
  314. return;
  315. }
  316. if (parts.size() == 4) {
  317. auto top = parse_css_value(parts[0]);
  318. auto right = parse_css_value(parts[1]);
  319. auto bottom = parse_css_value(parts[2]);
  320. auto left = parse_css_value(parts[3]);
  321. style.set_property(CSS::PropertyID::PaddingTop, top);
  322. style.set_property(CSS::PropertyID::PaddingBottom, bottom);
  323. style.set_property(CSS::PropertyID::PaddingLeft, left);
  324. style.set_property(CSS::PropertyID::PaddingRight, right);
  325. return;
  326. }
  327. dbg() << "Unsure what to do with CSS padding value '" << value.to_string() << "'";
  328. return;
  329. }
  330. return;
  331. }
  332. if (property_id == CSS::PropertyID::ListStyle) {
  333. auto parts = split_on_whitespace(value.to_string());
  334. if (!parts.is_empty()) {
  335. auto value = parse_css_value(parts[0]);
  336. style.set_property(CSS::PropertyID::ListStyleType, value);
  337. }
  338. return;
  339. }
  340. style.set_property(property_id, value);
  341. }
  342. NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(const Element& element, const StyleProperties* parent_style) const
  343. {
  344. auto style = StyleProperties::create();
  345. if (parent_style) {
  346. parent_style->for_each_property([&](auto property_id, auto& value) {
  347. if (is_inherited_property(property_id))
  348. set_property_expanding_shorthands(style, property_id, value);
  349. });
  350. }
  351. element.apply_presentational_hints(*style);
  352. auto matching_rules = collect_matching_rules(element);
  353. for (auto& rule : matching_rules) {
  354. for (auto& property : rule.declaration().properties()) {
  355. set_property_expanding_shorthands(style, property.property_id, property.value);
  356. }
  357. }
  358. auto style_attribute = element.attribute("style");
  359. if (!style_attribute.is_null()) {
  360. if (auto declaration = parse_css_declaration(style_attribute)) {
  361. for (auto& property : declaration->properties()) {
  362. set_property_expanding_shorthands(style, property.property_id, property.value);
  363. }
  364. }
  365. }
  366. return style;
  367. }
  368. }