Selector.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. // https://www.w3.org/TR/selectors-4/#specificity-rules
  25. u32 Selector::specificity() const
  26. {
  27. if (m_specificity.has_value())
  28. return *m_specificity;
  29. unsigned ids = 0;
  30. unsigned tag_names = 0;
  31. unsigned classes = 0;
  32. for (auto& list : m_compound_selectors) {
  33. for (auto& simple_selector : list.simple_selectors) {
  34. switch (simple_selector.type) {
  35. case SimpleSelector::Type::Id:
  36. ++ids;
  37. break;
  38. case SimpleSelector::Type::Class:
  39. case SimpleSelector::Type::Attribute:
  40. case SimpleSelector::Type::PseudoClass:
  41. ++classes;
  42. break;
  43. case SimpleSelector::Type::TagName:
  44. case SimpleSelector::Type::PseudoElement:
  45. ++tag_names;
  46. break;
  47. default:
  48. break;
  49. }
  50. }
  51. }
  52. m_specificity = ids * 0x10000 + classes * 0x100 + tag_names;
  53. return *m_specificity;
  54. }
  55. // https://www.w3.org/TR/cssom/#serialize-a-simple-selector
  56. String Selector::SimpleSelector::serialize() const
  57. {
  58. StringBuilder s;
  59. switch (type) {
  60. case Selector::SimpleSelector::Type::TagName:
  61. case Selector::SimpleSelector::Type::Universal:
  62. // 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.
  63. // FIXME: 2. If the namespace prefix maps to a namespace that is the null namespace (not in a namespace) append "|" (U+007C) to s.
  64. // 3. If this is a type selector append the serialization of the element name as an identifier to s.
  65. if (type == Selector::SimpleSelector::Type::TagName) {
  66. serialize_an_identifier(s, value);
  67. }
  68. // 4. If this is a universal selector append "*" (U+002A) to s.
  69. if (type == Selector::SimpleSelector::Type::Universal)
  70. s.append('*');
  71. break;
  72. case Selector::SimpleSelector::Type::Attribute:
  73. // 1. Append "[" (U+005B) to s.
  74. s.append('[');
  75. // 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.
  76. // 3. Append the serialization of the attribute name as an identifier to s.
  77. serialize_an_identifier(s, attribute.name);
  78. // 4. If there is an attribute value specified, append "=", "~=", "|=", "^=", "$=", or "*=" as appropriate (depending on the type of attribute selector),
  79. // followed by the serialization of the attribute value as a string, to s.
  80. if (!attribute.value.is_null()) {
  81. switch (attribute.match_type) {
  82. case Selector::SimpleSelector::Attribute::MatchType::ExactValueMatch:
  83. s.append("=");
  84. break;
  85. case Selector::SimpleSelector::Attribute::MatchType::ContainsWord:
  86. s.append("~=");
  87. break;
  88. case Selector::SimpleSelector::Attribute::MatchType::ContainsString:
  89. s.append("*=");
  90. break;
  91. case Selector::SimpleSelector::Attribute::MatchType::StartsWithSegment:
  92. s.append("|=");
  93. break;
  94. case Selector::SimpleSelector::Attribute::MatchType::StartsWithString:
  95. s.append("^=");
  96. break;
  97. case Selector::SimpleSelector::Attribute::MatchType::EndsWithString:
  98. s.append("$=");
  99. break;
  100. default:
  101. break;
  102. }
  103. serialize_a_string(s, attribute.value);
  104. }
  105. // FIXME: 5. If the attribute selector has the case-sensitivity flag present, append " i" (U+0020 U+0069) to s.
  106. // 6. Append "]" (U+005D) to s.
  107. s.append(']');
  108. break;
  109. case Selector::SimpleSelector::Type::Class:
  110. // Append a "." (U+002E), followed by the serialization of the class name as an identifier to s.
  111. s.append('.');
  112. serialize_an_identifier(s, value);
  113. break;
  114. case Selector::SimpleSelector::Type::Id:
  115. // Append a "#" (U+0023), followed by the serialization of the ID as an identifier to s.
  116. s.append('#');
  117. serialize_an_identifier(s, value);
  118. break;
  119. case Selector::SimpleSelector::Type::PseudoClass:
  120. switch (pseudo_class.type) {
  121. case Selector::SimpleSelector::PseudoClass::Type::Link:
  122. case Selector::SimpleSelector::PseudoClass::Type::Visited:
  123. case Selector::SimpleSelector::PseudoClass::Type::Hover:
  124. case Selector::SimpleSelector::PseudoClass::Type::Focus:
  125. case Selector::SimpleSelector::PseudoClass::Type::FirstChild:
  126. case Selector::SimpleSelector::PseudoClass::Type::LastChild:
  127. case Selector::SimpleSelector::PseudoClass::Type::OnlyChild:
  128. case Selector::SimpleSelector::PseudoClass::Type::Empty:
  129. case Selector::SimpleSelector::PseudoClass::Type::Root:
  130. case Selector::SimpleSelector::PseudoClass::Type::FirstOfType:
  131. case Selector::SimpleSelector::PseudoClass::Type::LastOfType:
  132. case Selector::SimpleSelector::PseudoClass::Type::OnlyOfType:
  133. case Selector::SimpleSelector::PseudoClass::Type::Disabled:
  134. case Selector::SimpleSelector::PseudoClass::Type::Enabled:
  135. case Selector::SimpleSelector::PseudoClass::Type::Checked:
  136. case Selector::SimpleSelector::PseudoClass::Type::Active:
  137. // If the pseudo-class does not accept arguments append ":" (U+003A), followed by the name of the pseudo-class, to s.
  138. s.append(':');
  139. s.append(pseudo_class_name(pseudo_class.type));
  140. break;
  141. case Selector::SimpleSelector::PseudoClass::Type::NthChild:
  142. case Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
  143. case Selector::SimpleSelector::PseudoClass::Type::Not:
  144. case Selector::SimpleSelector::PseudoClass::Type::Is:
  145. // Otherwise, append ":" (U+003A), followed by the name of the pseudo-class, followed by "(" (U+0028),
  146. // followed by the value of the pseudo-class argument(s) determined as per below, followed by ")" (U+0029), to s.
  147. s.append(':');
  148. s.append(pseudo_class_name(pseudo_class.type));
  149. s.append('(');
  150. if (pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::NthChild
  151. || pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::NthLastChild) {
  152. // The result of serializing the value using the rules to serialize an <an+b> value.
  153. s.append(pseudo_class.nth_child_pattern.serialize());
  154. } else if (pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::Not
  155. || pseudo_class.type == Selector::SimpleSelector::PseudoClass::Type::Is) {
  156. // The result of serializing the value using the rules for serializing a group of selectors.
  157. // NOTE: `:is()` isn't in the spec for this yet, but it should be!
  158. s.append(serialize_a_group_of_selectors(pseudo_class.argument_selector_list));
  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. }