Selector.cpp 12 KB

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