Selector.cpp 13 KB

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