Selector.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "Selector.h"
  8. #include <LibWeb/CSS/Serialize.h>
  9. namespace Web::CSS {
  10. Selector::Selector(Vector<CompoundSelector>&& compound_selectors)
  11. : m_compound_selectors(move(compound_selectors))
  12. {
  13. // Note: This assumes that only one pseudo-element is allowed in a selector, and that it appears at the end.
  14. // This is true currently, and there are no current proposals to change this, but you never know!
  15. if (!m_compound_selectors.is_empty()) {
  16. for (auto const& simple_selector : m_compound_selectors.last().simple_selectors) {
  17. if (simple_selector.type == SimpleSelector::Type::PseudoElement) {
  18. m_pseudo_element = simple_selector.pseudo_element;
  19. break;
  20. }
  21. }
  22. }
  23. }
  24. // https://www.w3.org/TR/selectors-4/#specificity-rules
  25. u32 Selector::specificity() const
  26. {
  27. if (m_specificity.has_value())
  28. return *m_specificity;
  29. constexpr u32 ids_shift = 16;
  30. constexpr u32 classes_shift = 8;
  31. constexpr u32 tag_names_shift = 0;
  32. constexpr u32 ids_mask = 0xff << ids_shift;
  33. constexpr u32 classes_mask = 0xff << classes_shift;
  34. constexpr u32 tag_names_mask = 0xff << tag_names_shift;
  35. u32 ids = 0;
  36. u32 classes = 0;
  37. u32 tag_names = 0;
  38. auto count_specificity_of_most_complex_selector = [&](auto& selector_list) {
  39. u32 max_selector_list_argument_specificity = 0;
  40. for (auto const& complex_selector : selector_list) {
  41. max_selector_list_argument_specificity = max(max_selector_list_argument_specificity, complex_selector.specificity());
  42. }
  43. u32 child_ids = (max_selector_list_argument_specificity & ids_mask) >> ids_shift;
  44. u32 child_classes = (max_selector_list_argument_specificity & classes_mask) >> classes_shift;
  45. u32 child_tag_names = (max_selector_list_argument_specificity & tag_names_mask) >> tag_names_shift;
  46. ids += child_ids;
  47. classes += child_classes;
  48. tag_names += child_tag_names;
  49. };
  50. for (auto& list : m_compound_selectors) {
  51. for (auto& simple_selector : list.simple_selectors) {
  52. switch (simple_selector.type) {
  53. case SimpleSelector::Type::Id:
  54. // count the number of ID selectors in the selector (= A)
  55. ++ids;
  56. break;
  57. case SimpleSelector::Type::Class:
  58. case SimpleSelector::Type::Attribute:
  59. // count the number of class selectors, attributes selectors, and pseudo-classes in the selector (= B)
  60. ++classes;
  61. break;
  62. case SimpleSelector::Type::PseudoClass:
  63. switch (simple_selector.pseudo_class.type) {
  64. case SimpleSelector::PseudoClass::Type::Is:
  65. case SimpleSelector::PseudoClass::Type::Not: {
  66. // The specificity of an :is(), :not(), or :has() pseudo-class is replaced by the
  67. // specificity of the most specific complex selector in its selector list argument.
  68. count_specificity_of_most_complex_selector(simple_selector.pseudo_class.argument_selector_list);
  69. break;
  70. }
  71. case SimpleSelector::PseudoClass::Type::NthChild:
  72. case SimpleSelector::PseudoClass::Type::NthLastChild: {
  73. // Analogously, the specificity of an :nth-child() or :nth-last-child() selector
  74. // is the specificity of the pseudo class itself (counting as one pseudo-class selector)
  75. // plus the specificity of the most specific complex selector in its selector list argument (if any).
  76. ++classes;
  77. count_specificity_of_most_complex_selector(simple_selector.pseudo_class.argument_selector_list);
  78. break;
  79. }
  80. case SimpleSelector::PseudoClass::Type::Where:
  81. // The specificity of a :where() pseudo-class is replaced by zero.
  82. break;
  83. default:
  84. ++classes;
  85. break;
  86. }
  87. break;
  88. case SimpleSelector::Type::TagName:
  89. case SimpleSelector::Type::PseudoElement:
  90. // count the number of type selectors and pseudo-elements in the selector (= C)
  91. ++tag_names;
  92. break;
  93. case SimpleSelector::Type::Universal:
  94. // ignore the universal selector
  95. break;
  96. case SimpleSelector::Type::Invalid:
  97. break;
  98. }
  99. }
  100. }
  101. // Due to storage limitations, implementations may have limitations on the size of A, B, or C.
  102. // If so, values higher than the limit must be clamped to that limit, and not overflow.
  103. m_specificity = (min(ids, 0xff) << ids_shift)
  104. + (min(classes, 0xff) << classes_shift)
  105. + (min(tag_names, 0xff) << tag_names_shift);
  106. return *m_specificity;
  107. }
  108. // https://www.w3.org/TR/cssom/#serialize-a-simple-selector
  109. String Selector::SimpleSelector::serialize() const
  110. {
  111. StringBuilder s;
  112. switch (type) {
  113. case Selector::SimpleSelector::Type::TagName:
  114. case Selector::SimpleSelector::Type::Universal:
  115. // 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.
  116. // FIXME: 2. If the namespace prefix maps to a namespace that is the null namespace (not in a namespace) append "|" (U+007C) to s.
  117. // 3. If this is a type selector append the serialization of the element name as an identifier to s.
  118. if (type == Selector::SimpleSelector::Type::TagName) {
  119. serialize_an_identifier(s, value);
  120. }
  121. // 4. If this is a universal selector append "*" (U+002A) to s.
  122. if (type == Selector::SimpleSelector::Type::Universal)
  123. s.append('*');
  124. break;
  125. case Selector::SimpleSelector::Type::Attribute:
  126. // 1. Append "[" (U+005B) to s.
  127. s.append('[');
  128. // 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.
  129. // 3. Append the serialization of the attribute name as an identifier to s.
  130. serialize_an_identifier(s, attribute.name);
  131. // 4. If there is an attribute value specified, append "=", "~=", "|=", "^=", "$=", or "*=" as appropriate (depending on the type of attribute selector),
  132. // followed by the serialization of the attribute value as a string, to s.
  133. if (!attribute.value.is_null()) {
  134. switch (attribute.match_type) {
  135. case Selector::SimpleSelector::Attribute::MatchType::ExactValueMatch:
  136. s.append("=");
  137. break;
  138. case Selector::SimpleSelector::Attribute::MatchType::ContainsWord:
  139. s.append("~=");
  140. break;
  141. case Selector::SimpleSelector::Attribute::MatchType::ContainsString:
  142. s.append("*=");
  143. break;
  144. case Selector::SimpleSelector::Attribute::MatchType::StartsWithSegment:
  145. s.append("|=");
  146. break;
  147. case Selector::SimpleSelector::Attribute::MatchType::StartsWithString:
  148. s.append("^=");
  149. break;
  150. case Selector::SimpleSelector::Attribute::MatchType::EndsWithString:
  151. s.append("$=");
  152. break;
  153. default:
  154. break;
  155. }
  156. serialize_a_string(s, attribute.value);
  157. }
  158. // FIXME: 5. If the attribute selector has the case-sensitivity flag present, append " i" (U+0020 U+0069) to s.
  159. // 6. Append "]" (U+005D) to s.
  160. s.append(']');
  161. break;
  162. case Selector::SimpleSelector::Type::Class:
  163. // Append a "." (U+002E), followed by the serialization of the class name as an identifier to s.
  164. s.append('.');
  165. serialize_an_identifier(s, value);
  166. break;
  167. case Selector::SimpleSelector::Type::Id:
  168. // Append a "#" (U+0023), followed by the serialization of the ID as an identifier to s.
  169. s.append('#');
  170. serialize_an_identifier(s, value);
  171. break;
  172. case Selector::SimpleSelector::Type::PseudoClass:
  173. switch (pseudo_class.type) {
  174. case Selector::SimpleSelector::PseudoClass::Type::Link:
  175. case Selector::SimpleSelector::PseudoClass::Type::Visited:
  176. case Selector::SimpleSelector::PseudoClass::Type::Hover:
  177. case Selector::SimpleSelector::PseudoClass::Type::Focus:
  178. case Selector::SimpleSelector::PseudoClass::Type::FirstChild:
  179. case Selector::SimpleSelector::PseudoClass::Type::LastChild:
  180. case Selector::SimpleSelector::PseudoClass::Type::OnlyChild:
  181. case Selector::SimpleSelector::PseudoClass::Type::Empty:
  182. case Selector::SimpleSelector::PseudoClass::Type::Root:
  183. case Selector::SimpleSelector::PseudoClass::Type::FirstOfType:
  184. case Selector::SimpleSelector::PseudoClass::Type::LastOfType:
  185. case Selector::SimpleSelector::PseudoClass::Type::OnlyOfType:
  186. case Selector::SimpleSelector::PseudoClass::Type::Disabled:
  187. case Selector::SimpleSelector::PseudoClass::Type::Enabled:
  188. case Selector::SimpleSelector::PseudoClass::Type::Checked:
  189. case Selector::SimpleSelector::PseudoClass::Type::Active:
  190. // If the pseudo-class does not accept arguments append ":" (U+003A), followed by the name of the pseudo-class, to s.
  191. s.append(':');
  192. s.append(pseudo_class_name(pseudo_class.type));
  193. break;
  194. case Selector::SimpleSelector::PseudoClass::Type::NthChild:
  195. case Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
  196. case Selector::SimpleSelector::PseudoClass::Type::Not:
  197. case Selector::SimpleSelector::PseudoClass::Type::Is:
  198. case Selector::SimpleSelector::PseudoClass::Type::Where:
  199. // Otherwise, append ":" (U+003A), followed by the name of the pseudo-class, followed by "(" (U+0028),
  200. // followed by the value of the pseudo-class argument(s) determined as per below, followed by ")" (U+0029), to s.
  201. s.append(':');
  202. s.append(pseudo_class_name(pseudo_class.type));
  203. s.append('(');
  204. if (pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::NthChild
  205. || pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::NthLastChild) {
  206. // The result of serializing the value using the rules to serialize an <an+b> value.
  207. s.append(pseudo_class.nth_child_pattern.serialize());
  208. } else if (pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::Not
  209. || pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::Is
  210. || pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::Where) {
  211. // The result of serializing the value using the rules for serializing a group of selectors.
  212. // NOTE: `:is()` and `:where()` aren't in the spec for this yet, but it should be!
  213. s.append(serialize_a_group_of_selectors(pseudo_class.argument_selector_list));
  214. }
  215. s.append(')');
  216. break;
  217. default:
  218. VERIFY_NOT_REACHED();
  219. }
  220. break;
  221. case Selector::SimpleSelector::Type::PseudoElement:
  222. // Note: Pseudo-elements are dealt with in Selector::serialize()
  223. break;
  224. default:
  225. dbgln("FIXME: Unsupported simple selector serialization for type {}", to_underlying(type));
  226. break;
  227. }
  228. return s.to_string();
  229. }
  230. // https://www.w3.org/TR/cssom/#serialize-a-selector
  231. String Selector::serialize() const
  232. {
  233. StringBuilder s;
  234. // 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:
  235. for (size_t i = 0; i < compound_selectors().size(); ++i) {
  236. auto const& compound_selector = compound_selectors()[i];
  237. // 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.
  238. if (compound_selector.simple_selectors.size() == 1
  239. && compound_selector.simple_selectors.first().type == Selector::SimpleSelector::Type::Universal) {
  240. s.append(compound_selector.simple_selectors.first().serialize());
  241. }
  242. // 2. Otherwise, for each simple selector in the compound selectors...
  243. // FIXME: ...that is not a universal selector of which the namespace prefix maps to a namespace that is not the default namespace...
  244. // ...serialize the simple selector and append the result to s.
  245. else {
  246. for (auto& simple_selector : compound_selector.simple_selectors) {
  247. s.append(simple_selector.serialize());
  248. }
  249. }
  250. // 3. If this is not the last part of the chain of the selector append a single SPACE (U+0020),
  251. // followed by the combinator ">", "+", "~", ">>", "||", as appropriate, followed by another
  252. // single SPACE (U+0020) if the combinator was not whitespace, to s.
  253. if (i != compound_selectors().size() - 1) {
  254. s.append(' ');
  255. // Note: The combinator that appears between parts `i` and `i+1` appears with the `i+1` selector,
  256. // so we have to check that one.
  257. switch (compound_selectors()[i + 1].combinator) {
  258. case Selector::Combinator::ImmediateChild:
  259. s.append("> ");
  260. break;
  261. case Selector::Combinator::NextSibling:
  262. s.append("+ ");
  263. break;
  264. case Selector::Combinator::SubsequentSibling:
  265. s.append("~ ");
  266. break;
  267. case Selector::Combinator::Column:
  268. s.append("|| ");
  269. break;
  270. default:
  271. break;
  272. }
  273. } else {
  274. // 4. If this is the last part of the chain of the selector and there is a pseudo-element,
  275. // append "::" followed by the name of the pseudo-element, to s.
  276. if (compound_selector.simple_selectors.last().type == Selector::SimpleSelector::Type::PseudoElement) {
  277. s.append("::");
  278. s.append(pseudo_element_name(compound_selector.simple_selectors.last().pseudo_element));
  279. }
  280. }
  281. }
  282. return s.to_string();
  283. }
  284. // https://www.w3.org/TR/cssom/#serialize-a-group-of-selectors
  285. String serialize_a_group_of_selectors(NonnullRefPtrVector<Selector> const& selectors)
  286. {
  287. // To serialize a group of selectors serialize each selector in the group of selectors and then serialize a comma-separated list of these serializations.
  288. StringBuilder builder;
  289. builder.join(", ", selectors);
  290. return builder.to_string();
  291. }
  292. Optional<Selector::PseudoElement> pseudo_element_from_string(StringView name)
  293. {
  294. if (name.equals_ignoring_case("after")) {
  295. return Selector::PseudoElement::After;
  296. } else if (name.equals_ignoring_case("before")) {
  297. return Selector::PseudoElement::Before;
  298. } else if (name.equals_ignoring_case("first-letter")) {
  299. return Selector::PseudoElement::FirstLetter;
  300. } else if (name.equals_ignoring_case("first-line")) {
  301. return Selector::PseudoElement::FirstLine;
  302. } else if (name.equals_ignoring_case("marker")) {
  303. return Selector::PseudoElement::Marker;
  304. }
  305. return {};
  306. }
  307. }