Selector.cpp 13 KB

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