Selector.cpp 12 KB

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