Selector.cpp 14 KB

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