Selector.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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::Disabled:
  121. case Selector::SimpleSelector::PseudoClass::Type::Enabled:
  122. case Selector::SimpleSelector::PseudoClass::Type::Checked:
  123. case Selector::SimpleSelector::PseudoClass::Type::Active:
  124. // If the pseudo-class does not accept arguments append ":" (U+003A), followed by the name of the pseudo-class, to s.
  125. s.append(':');
  126. s.append(pseudo_class_name(pseudo_class.type));
  127. break;
  128. case Selector::SimpleSelector::PseudoClass::Type::NthChild:
  129. case Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
  130. case Selector::SimpleSelector::PseudoClass::Type::Not:
  131. // Otherwise, append ":" (U+003A), followed by the name of the pseudo-class, followed by "(" (U+0028),
  132. // followed by the value of the pseudo-class argument(s) determined as per below, followed by ")" (U+0029), to s.
  133. s.append(':');
  134. s.append(pseudo_class_name(pseudo_class.type));
  135. s.append('(');
  136. if (pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::NthChild
  137. || pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::NthLastChild) {
  138. // The result of serializing the value using the rules to serialize an <an+b> value.
  139. s.append(pseudo_class.nth_child_pattern.serialize());
  140. } else if (pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::Not) {
  141. // The result of serializing the value using the rules for serializing a group of selectors.
  142. s.append(serialize_a_group_of_selectors(pseudo_class.not_selector));
  143. }
  144. s.append(')');
  145. break;
  146. default:
  147. VERIFY_NOT_REACHED();
  148. }
  149. break;
  150. default:
  151. dbgln("FIXME: Unsupported simple selector serialization for type {}", to_underlying(type));
  152. break;
  153. }
  154. return s.to_string();
  155. }
  156. // https://www.w3.org/TR/cssom/#serialize-a-selector
  157. String Selector::serialize() const
  158. {
  159. StringBuilder s;
  160. // 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:
  161. for (size_t i = 0; i < compound_selectors().size(); ++i) {
  162. auto const& compound_selector = compound_selectors()[i];
  163. // 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.
  164. if (compound_selector.simple_selectors.size() == 1
  165. && compound_selector.simple_selectors.first().type == Selector::SimpleSelector::Type::Universal) {
  166. s.append(compound_selectors().first().simple_selectors.first().serialize());
  167. }
  168. // 2. Otherwise, for each simple selector in the compound selectors...
  169. // FIXME: ...that is not a universal selector of which the namespace prefix maps to a namespace that is not the default namespace...
  170. // ...serialize the simple selector and append the result to s.
  171. else {
  172. for (auto& simple_selector : compound_selector.simple_selectors) {
  173. s.append(simple_selector.serialize());
  174. }
  175. }
  176. // 3. If this is not the last part of the chain of the selector append a single SPACE (U+0020),
  177. // followed by the combinator ">", "+", "~", ">>", "||", as appropriate, followed by another
  178. // single SPACE (U+0020) if the combinator was not whitespace, to s.
  179. if (i != compound_selectors().size() - 1) {
  180. s.append(' ');
  181. // Note: The combinator that appears between parts `i` and `i+1` appears with the `i+1` selector,
  182. // so we have to check that one.
  183. switch (compound_selectors()[i + 1].combinator) {
  184. case Selector::Combinator::ImmediateChild:
  185. s.append("> ");
  186. break;
  187. case Selector::Combinator::NextSibling:
  188. s.append("+ ");
  189. break;
  190. case Selector::Combinator::SubsequentSibling:
  191. s.append("~ ");
  192. break;
  193. case Selector::Combinator::Column:
  194. s.append("|| ");
  195. break;
  196. default:
  197. break;
  198. }
  199. } else {
  200. // 4. If this is the last part of the chain of the selector and there is a pseudo-element,
  201. // append "::" followed by the name of the pseudo-element, to s.
  202. if (compound_selector.simple_selectors.last().type == Selector::SimpleSelector::Type::PseudoElement) {
  203. s.append("::");
  204. s.append(pseudo_element_name(compound_selector.simple_selectors.last().pseudo_element));
  205. }
  206. }
  207. }
  208. return s.to_string();
  209. }
  210. // https://www.w3.org/TR/cssom/#serialize-a-group-of-selectors
  211. String serialize_a_group_of_selectors(NonnullRefPtrVector<Selector> const& selectors)
  212. {
  213. // To serialize a group of selectors serialize each selector in the group of selectors and then serialize a comma-separated list of these serializations.
  214. StringBuilder builder;
  215. builder.join(", ", selectors);
  216. return builder.to_string();
  217. }
  218. constexpr StringView pseudo_element_name(Selector::SimpleSelector::PseudoElement pseudo_element)
  219. {
  220. switch (pseudo_element) {
  221. case Selector::SimpleSelector::PseudoElement::Before:
  222. return "before"sv;
  223. case Selector::SimpleSelector::PseudoElement::After:
  224. return "after"sv;
  225. case Selector::SimpleSelector::PseudoElement::FirstLine:
  226. return "first-line"sv;
  227. case Selector::SimpleSelector::PseudoElement::FirstLetter:
  228. return "first-letter"sv;
  229. case Selector::SimpleSelector::PseudoElement::None:
  230. break;
  231. }
  232. VERIFY_NOT_REACHED();
  233. }
  234. constexpr StringView pseudo_class_name(Selector::SimpleSelector::PseudoClass::Type pseudo_class)
  235. {
  236. switch (pseudo_class) {
  237. case Selector::SimpleSelector::PseudoClass::Type::Link:
  238. return "link"sv;
  239. case Selector::SimpleSelector::PseudoClass::Type::Visited:
  240. return "visited"sv;
  241. case Selector::SimpleSelector::PseudoClass::Type::Hover:
  242. return "hover"sv;
  243. case Selector::SimpleSelector::PseudoClass::Type::Focus:
  244. return "focus"sv;
  245. case Selector::SimpleSelector::PseudoClass::Type::FirstChild:
  246. return "first-child"sv;
  247. case Selector::SimpleSelector::PseudoClass::Type::LastChild:
  248. return "last-child"sv;
  249. case Selector::SimpleSelector::PseudoClass::Type::OnlyChild:
  250. return "only-child"sv;
  251. case Selector::SimpleSelector::PseudoClass::Type::Empty:
  252. return "empty"sv;
  253. case Selector::SimpleSelector::PseudoClass::Type::Root:
  254. return "root"sv;
  255. case Selector::SimpleSelector::PseudoClass::Type::FirstOfType:
  256. return "first-of-type"sv;
  257. case Selector::SimpleSelector::PseudoClass::Type::LastOfType:
  258. return "last-of-type"sv;
  259. case Selector::SimpleSelector::PseudoClass::Type::Disabled:
  260. return "disabled"sv;
  261. case Selector::SimpleSelector::PseudoClass::Type::Enabled:
  262. return "enabled"sv;
  263. case Selector::SimpleSelector::PseudoClass::Type::Checked:
  264. return "checked"sv;
  265. case Selector::SimpleSelector::PseudoClass::Type::Active:
  266. return "active"sv;
  267. case Selector::SimpleSelector::PseudoClass::Type::NthChild:
  268. return "nth-child"sv;
  269. case Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
  270. return "nth-last-child"sv;
  271. case Selector::SimpleSelector::PseudoClass::Type::Not:
  272. return "not"sv;
  273. case Selector::SimpleSelector::PseudoClass::Type::None:
  274. break;
  275. }
  276. VERIFY_NOT_REACHED();
  277. }
  278. }