Selector.cpp 13 KB

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