CSSStyleRule.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/CSSStyleRule.h>
  7. namespace Web::CSS {
  8. CSSStyleRule::CSSStyleRule(NonnullRefPtrVector<Selector>&& selectors, NonnullRefPtr<CSSStyleDeclaration>&& declaration)
  9. : m_selectors(move(selectors))
  10. , m_declaration(move(declaration))
  11. {
  12. }
  13. CSSStyleRule::~CSSStyleRule()
  14. {
  15. }
  16. // https://drafts.csswg.org/cssom/#dom-cssstylerule-style
  17. CSSStyleDeclaration* CSSStyleRule::style()
  18. {
  19. return m_declaration;
  20. }
  21. static StringView to_string(Selector::SimpleSelector::PseudoElement pseudo_element)
  22. {
  23. switch (pseudo_element) {
  24. case Selector::SimpleSelector::PseudoElement::Before:
  25. return "before"sv;
  26. case Selector::SimpleSelector::PseudoElement::After:
  27. return "after"sv;
  28. case Selector::SimpleSelector::PseudoElement::FirstLine:
  29. return "first-line"sv;
  30. case Selector::SimpleSelector::PseudoElement::FirstLetter:
  31. return "first-letter"sv;
  32. case Selector::SimpleSelector::PseudoElement::None:
  33. break;
  34. }
  35. VERIFY_NOT_REACHED();
  36. }
  37. static StringView to_string(Selector::SimpleSelector::PseudoClass::Type pseudo_class)
  38. {
  39. switch (pseudo_class) {
  40. case Selector::SimpleSelector::PseudoClass::Type::Link:
  41. return "link"sv;
  42. case Selector::SimpleSelector::PseudoClass::Type::Visited:
  43. return "visited"sv;
  44. case Selector::SimpleSelector::PseudoClass::Type::Hover:
  45. return "hover"sv;
  46. case Selector::SimpleSelector::PseudoClass::Type::Focus:
  47. return "focus"sv;
  48. case Selector::SimpleSelector::PseudoClass::Type::FirstChild:
  49. return "first-child"sv;
  50. case Selector::SimpleSelector::PseudoClass::Type::LastChild:
  51. return "last-child"sv;
  52. case Selector::SimpleSelector::PseudoClass::Type::OnlyChild:
  53. return "only-child"sv;
  54. case Selector::SimpleSelector::PseudoClass::Type::Empty:
  55. return "empty"sv;
  56. case Selector::SimpleSelector::PseudoClass::Type::Root:
  57. return "root"sv;
  58. case Selector::SimpleSelector::PseudoClass::Type::FirstOfType:
  59. return "first-of-pseudo_class"sv;
  60. case Selector::SimpleSelector::PseudoClass::Type::LastOfType:
  61. return "last-of-pseudo_class"sv;
  62. case Selector::SimpleSelector::PseudoClass::Type::Disabled:
  63. return "disabled"sv;
  64. case Selector::SimpleSelector::PseudoClass::Type::Enabled:
  65. return "enabled"sv;
  66. case Selector::SimpleSelector::PseudoClass::Type::Checked:
  67. return "checked"sv;
  68. case Selector::SimpleSelector::PseudoClass::Type::Active:
  69. return "active"sv;
  70. case Selector::SimpleSelector::PseudoClass::Type::NthChild:
  71. return "nth-child"sv;
  72. case Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
  73. return "nth-last-child"sv;
  74. case Selector::SimpleSelector::PseudoClass::Type::Not:
  75. return "not"sv;
  76. case Selector::SimpleSelector::PseudoClass::Type::None:
  77. break;
  78. }
  79. VERIFY_NOT_REACHED();
  80. }
  81. static String serialize_a_group_of_selectors(NonnullRefPtrVector<Selector> const&);
  82. // https://drafts.csswg.org/cssom/#serialize-a-simple-selector
  83. static String serialize_a_simple_selector(Selector::SimpleSelector const& simple_selector)
  84. {
  85. StringBuilder builder;
  86. switch (simple_selector.type) {
  87. case Selector::SimpleSelector::Type::TagName:
  88. case Selector::SimpleSelector::Type::Universal:
  89. // FIXME: 1. If the namespace prefix maps to a namespace that is not the default namespace and is not the null namespace (not in a namespace) append the serialization of the namespace prefix as an identifier, followed by a "|" (U+007C) to s.
  90. // FIXME: 2. If the namespace prefix maps to a namespace that is the null namespace (not in a namespace) append "|" (U+007C) to s.
  91. // 3. If this is a type selector append the serialization of the element name as an identifier to s.
  92. if (simple_selector.type == Selector::SimpleSelector::Type::TagName) {
  93. // FIXME: Use the "serialize an identifier" algorithm.
  94. builder.append(simple_selector.value);
  95. }
  96. // 4. If this is a universal selector append "*" (U+002A) to s.
  97. if (simple_selector.type == Selector::SimpleSelector::Type::Universal)
  98. builder.append('*');
  99. break;
  100. case Selector::SimpleSelector::Type::Attribute:
  101. // 1. Append "[" (U+005B) to s.
  102. builder.append('[');
  103. // FIXME: 2. If the namespace prefix maps to a namespace that is not the null namespace (not in a namespace) append the serialization of the namespace prefix as an identifier, followed by a "|" (U+007C) to s.
  104. // 3. Append the serialization of the attribute name as an identifier to s.
  105. // FIXME: Use the "serialize an identifier" algorithm.
  106. builder.append(simple_selector.attribute.name);
  107. // 4. If there is an attribute value specified, append "=", "~=", "|=", "^=", "$=", or "*=" as appropriate (depending on the type of attribute selector),
  108. // followed by the serialization of the attribute value as a string, to s.
  109. if (!simple_selector.attribute.value.is_null()) {
  110. switch (simple_selector.attribute.match_type) {
  111. case Selector::SimpleSelector::Attribute::MatchType::ExactValueMatch:
  112. builder.append("=");
  113. break;
  114. case Selector::SimpleSelector::Attribute::MatchType::ContainsWord:
  115. builder.append("~=");
  116. break;
  117. case Selector::SimpleSelector::Attribute::MatchType::ContainsString:
  118. builder.append("*=");
  119. break;
  120. case Selector::SimpleSelector::Attribute::MatchType::StartsWithSegment:
  121. builder.append("|=");
  122. break;
  123. case Selector::SimpleSelector::Attribute::MatchType::StartsWithString:
  124. builder.append("^=");
  125. break;
  126. case Selector::SimpleSelector::Attribute::MatchType::EndsWithString:
  127. builder.append("$=");
  128. break;
  129. default:
  130. break;
  131. }
  132. }
  133. // FIXME: 5. If the attribute selector has the case-sensitivity flag present, append " i" (U+0020 U+0069) to s.
  134. // 6. Append "]" (U+005D) to s.
  135. builder.append(']');
  136. break;
  137. case Selector::SimpleSelector::Type::Class:
  138. // Append a "." (U+002E), followed by the serialization of the class name as an identifier to s.
  139. builder.append('.');
  140. // FIXME: Use the "serialize an identifier" algorithm.
  141. builder.append(simple_selector.value);
  142. break;
  143. case Selector::SimpleSelector::Type::Id:
  144. // Append a "#" (U+0023), followed by the serialization of the ID as an identifier to s.
  145. builder.append('#');
  146. // FIXME: Use the "serialize an identifier" algorithm.
  147. builder.append(simple_selector.value);
  148. break;
  149. case Selector::SimpleSelector::Type::PseudoClass:
  150. switch (simple_selector.pseudo_class.type) {
  151. case Selector::SimpleSelector::PseudoClass::Type::Link:
  152. case Selector::SimpleSelector::PseudoClass::Type::Visited:
  153. case Selector::SimpleSelector::PseudoClass::Type::Hover:
  154. case Selector::SimpleSelector::PseudoClass::Type::Focus:
  155. case Selector::SimpleSelector::PseudoClass::Type::FirstChild:
  156. case Selector::SimpleSelector::PseudoClass::Type::LastChild:
  157. case Selector::SimpleSelector::PseudoClass::Type::OnlyChild:
  158. case Selector::SimpleSelector::PseudoClass::Type::Empty:
  159. case Selector::SimpleSelector::PseudoClass::Type::Root:
  160. case Selector::SimpleSelector::PseudoClass::Type::FirstOfType:
  161. case Selector::SimpleSelector::PseudoClass::Type::LastOfType:
  162. case Selector::SimpleSelector::PseudoClass::Type::Disabled:
  163. case Selector::SimpleSelector::PseudoClass::Type::Enabled:
  164. case Selector::SimpleSelector::PseudoClass::Type::Checked:
  165. case Selector::SimpleSelector::PseudoClass::Type::Active:
  166. // If the pseudo-class does not accept arguments append ":" (U+003A), followed by the name of the pseudo-class, to s.
  167. builder.append(':');
  168. builder.append(to_string(simple_selector.pseudo_class.type));
  169. break;
  170. case Selector::SimpleSelector::PseudoClass::Type::NthChild:
  171. case Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
  172. case Selector::SimpleSelector::PseudoClass::Type::Not:
  173. // Otherwise, append ":" (U+003A), followed by the name of the pseudo-class, followed by "(" (U+0028),
  174. // followed by the value of the pseudo-class argument(s) determined as per below, followed by ")" (U+0029), to s.
  175. builder.append(':');
  176. builder.append(to_string(simple_selector.pseudo_class.type));
  177. builder.append('(');
  178. if (simple_selector.pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::NthChild
  179. || simple_selector.pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::NthLastChild) {
  180. // FIXME: The result of serializing the value using the rules to serialize an <an+b> value.
  181. TODO();
  182. } else if (simple_selector.pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::Not) {
  183. // The result of serializing the value using the rules for serializing a group of selectors.
  184. builder.append(serialize_a_group_of_selectors(simple_selector.pseudo_class.not_selector));
  185. }
  186. builder.append(')');
  187. break;
  188. default:
  189. VERIFY_NOT_REACHED();
  190. }
  191. break;
  192. default:
  193. dbgln("FIXME: Unsupported simple selector serialization for type {}", to_underlying(simple_selector.type));
  194. break;
  195. }
  196. return builder.to_string();
  197. }
  198. // https://drafts.csswg.org/cssom/#serialize-a-selector
  199. static String serialize_a_selector(Selector const& selector)
  200. {
  201. StringBuilder builder;
  202. // To serialize a selector let s be the empty string, run the steps below for each part of the chain of the selector, and finally return s:
  203. for (size_t i = 0; i < selector.compound_selectors().size(); ++i) {
  204. auto const& compound_selector = selector.compound_selectors()[i];
  205. // 1. If there is only one simple selector in the compound selectors which is a universal selector, append the result of serializing the universal selector to s.
  206. if (compound_selector.simple_selectors.size() == 1
  207. && compound_selector.simple_selectors.first().type == Selector::SimpleSelector::Type::Universal) {
  208. builder.append(serialize_a_simple_selector(selector.compound_selectors().first().simple_selectors.first()));
  209. }
  210. // 2. Otherwise, for each simple selector in the compound selectors...
  211. // FIXME: ...that is not a universal selector of which the namespace prefix maps to a namespace that is not the default namespace...
  212. // ...serialize the simple selector and append the result to s.
  213. else {
  214. for (auto& simple_selector : compound_selector.simple_selectors) {
  215. builder.append(serialize_a_simple_selector(simple_selector));
  216. }
  217. }
  218. // 3. If this is not the last part of the chain of the selector append a single SPACE (U+0020),
  219. // followed by the combinator ">", "+", "~", ">>", "||", as appropriate, followed by another
  220. // single SPACE (U+0020) if the combinator was not whitespace, to s.
  221. if (i != selector.compound_selectors().size() - 1) {
  222. builder.append(' ');
  223. switch (compound_selector.combinator) {
  224. case Selector::Combinator::ImmediateChild:
  225. builder.append('>');
  226. break;
  227. case Selector::Combinator::NextSibling:
  228. builder.append('+');
  229. break;
  230. case Selector::Combinator::SubsequentSibling:
  231. builder.append('~');
  232. break;
  233. case Selector::Combinator::Column:
  234. builder.append("||");
  235. break;
  236. default:
  237. break;
  238. }
  239. } else {
  240. // 4. If this is the last part of the chain of the selector and there is a pseudo-element, append "::" followed by the name of the pseudo-element, to s.
  241. // FIXME: This doesn't feel entirely correct. Our model of pseudo-elements seems off.
  242. if (!compound_selector.simple_selectors.is_empty()
  243. && compound_selector.simple_selectors.first().type == Selector::SimpleSelector::Type::PseudoElement) {
  244. builder.append("::");
  245. builder.append(to_string(compound_selector.simple_selectors.first().pseudo_element));
  246. }
  247. }
  248. }
  249. return builder.to_string();
  250. }
  251. // https://drafts.csswg.org/cssom/#serialize-a-group-of-selectors
  252. static String serialize_a_group_of_selectors(NonnullRefPtrVector<Selector> const& selectors)
  253. {
  254. // To serialize a group of selectors serialize each selector in the group of selectors and then serialize a comma-separated list of these serializations.
  255. StringBuilder builder;
  256. for (auto& selector : selectors)
  257. builder.append(serialize_a_selector(selector));
  258. return builder.to_string();
  259. }
  260. // https://drafts.csswg.org/cssom/#serialize-a-css-rule
  261. String CSSStyleRule::serialized() const
  262. {
  263. StringBuilder builder;
  264. // 1. Let s initially be the result of performing serialize a group of selectors on the rule’s associated selectors,
  265. // followed by the string " {", i.e., a single SPACE (U+0020), followed by LEFT CURLY BRACKET (U+007B).
  266. builder.append(serialize_a_group_of_selectors(selectors()));
  267. builder.append(" {"sv);
  268. // 2. Let decls be the result of performing serialize a CSS declaration block on the rule’s associated declarations, or null if there are no such declarations.
  269. auto decls = declaration().serialized();
  270. // FIXME: 3. Let rules be the result of performing serialize a CSS rule on each rule in the rule’s cssRules list, or null if there are no such rules.
  271. String rules;
  272. // 4. If decls and rules are both null, append " }" to s (i.e. a single SPACE (U+0020) followed by RIGHT CURLY BRACKET (U+007D)) and return s.
  273. if (decls.is_null() && rules.is_null()) {
  274. builder.append(" }"sv);
  275. return builder.to_string();
  276. }
  277. // 5. If rules is null:
  278. if (rules.is_null()) {
  279. // 1. Append a single SPACE (U+0020) to s
  280. builder.append(' ');
  281. // 2. Append decls to s
  282. builder.append(decls);
  283. // 3. Append " }" to s (i.e. a single SPACE (U+0020) followed by RIGHT CURLY BRACKET (U+007D)).
  284. builder.append(" }"sv);
  285. // 4. Return s.
  286. return builder.to_string();
  287. }
  288. // FIXME: 6. Otherwise:
  289. // FIXME: 1. If decls is not null, prepend it to rules.
  290. // FIXME: 2. For each rule in rules:
  291. // FIXME: 1. Append a newline followed by two spaces to s.
  292. // FIXME: 2. Append rule to s.
  293. // FIXME: 3. Append a newline followed by RIGHT CURLY BRACKET (U+007D) to s.
  294. // FIXME: 4. Return s.
  295. TODO();
  296. }
  297. // https://drafts.csswg.org/cssom/#dom-cssstylerule-selectortext
  298. String CSSStyleRule::selector_text() const
  299. {
  300. // The selectorText attribute, on getting, must return the result of serializing the associated group of selectors.
  301. return serialized();
  302. }
  303. // https://drafts.csswg.org/cssom/#dom-cssstylerule-selectortext
  304. void CSSStyleRule::set_selector_text(StringView selector_text)
  305. {
  306. // FIXME: 1. Run the parse a group of selectors algorithm on the given value.
  307. // FIXME: 2. If the algorithm returns a non-null value replace the associated group of selectors with the returned value.
  308. // FIXME: 3. Otherwise, if the algorithm returns a null value, do nothing.
  309. (void)selector_text;
  310. TODO();
  311. }
  312. }