StyleSheetSelector.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "StyleSheetSelector.h"
  29. #include "../../Include/RmlUi/Core/Element.h"
  30. #include "StyleSheetNode.h"
  31. #include <tuple>
  32. namespace Rml {
  33. static inline bool IsTextElement(const Element* element)
  34. {
  35. return element->GetTagName() == "#text";
  36. }
  37. // Returns true if a positive integer can be found for n in the equation an + b = count.
  38. static bool IsNth(int a, int b, int count)
  39. {
  40. int x = count;
  41. x -= b;
  42. if (a != 0)
  43. x /= a;
  44. return (x >= 0 && x * a + b == count);
  45. }
  46. bool operator==(const AttributeSelector& a, const AttributeSelector& b)
  47. {
  48. return a.type == b.type && a.name == b.name && a.value == b.value;
  49. }
  50. bool operator<(const AttributeSelector& a, const AttributeSelector& b)
  51. {
  52. return std::tie(a.type, a.name, a.value) < std::tie(b.type, b.name, b.value);
  53. }
  54. bool operator==(const StructuralSelector& a, const StructuralSelector& b)
  55. {
  56. // Currently sub-selectors (selector_tree) are only superficially compared. This mainly has the consequence that selectors with a sub-selector
  57. // which are instantiated separately will never compare equal, even if they have the exact same sub-selector expression. This further results in
  58. // such selectors not being de-duplicated. This should not lead to any functional differences but leads to potentially missed memory/performance
  59. // optimizations. E.g. 'div a, div b' will combine the two div nodes, while ':not(div) a, :not(div) b' will not combine the two not-div nodes.
  60. return a.type == b.type && a.a == b.a && a.b == b.b && a.selector_tree == b.selector_tree;
  61. }
  62. bool operator<(const StructuralSelector& a, const StructuralSelector& b)
  63. {
  64. return std::tie(a.type, a.a, a.b, a.selector_tree) < std::tie(b.type, b.a, b.b, b.selector_tree);
  65. }
  66. bool operator==(const CompoundSelector& a, const CompoundSelector& b)
  67. {
  68. if (a.tag != b.tag)
  69. return false;
  70. if (a.id != b.id)
  71. return false;
  72. if (a.class_names != b.class_names)
  73. return false;
  74. if (a.pseudo_class_names != b.pseudo_class_names)
  75. return false;
  76. if (a.attributes != b.attributes)
  77. return false;
  78. if (a.structural_selectors != b.structural_selectors)
  79. return false;
  80. if (a.combinator != b.combinator)
  81. return false;
  82. return true;
  83. }
  84. bool IsSelectorApplicable(const Element* element, const StructuralSelector& selector, const Element* scope)
  85. {
  86. RMLUI_ASSERT(element);
  87. switch (selector.type)
  88. {
  89. case StructuralSelectorType::Nth_Child:
  90. {
  91. Element* parent = element->GetParentNode();
  92. if (!parent)
  93. return false;
  94. // Start counting elements until we find this one.
  95. int element_index = 1;
  96. for (int i = 0; i < parent->GetNumChildren(); i++)
  97. {
  98. Element* child = parent->GetChild(i);
  99. // Skip text nodes.
  100. if (IsTextElement(child))
  101. continue;
  102. // If we've found our element, then break; the current index is our element's index.
  103. if (child == element)
  104. break;
  105. element_index++;
  106. }
  107. return IsNth(selector.a, selector.b, element_index);
  108. }
  109. break;
  110. case StructuralSelectorType::Nth_Last_Child:
  111. {
  112. Element* parent = element->GetParentNode();
  113. if (!parent)
  114. return false;
  115. // Start counting elements until we find this one.
  116. int element_index = 1;
  117. for (int i = parent->GetNumChildren() - 1; i >= 0; --i)
  118. {
  119. Element* child = parent->GetChild(i);
  120. // Skip text nodes.
  121. if (IsTextElement(child))
  122. continue;
  123. // If we've found our element, then break; the current index is our element's index.
  124. if (child == element)
  125. break;
  126. element_index++;
  127. }
  128. return IsNth(selector.a, selector.b, element_index);
  129. }
  130. break;
  131. case StructuralSelectorType::Nth_Of_Type:
  132. {
  133. Element* parent = element->GetParentNode();
  134. if (!parent)
  135. return false;
  136. // Start counting elements until we find this one.
  137. int element_index = 1;
  138. const int num_children = parent->GetNumChildren();
  139. for (int i = 0; i < num_children; i++)
  140. {
  141. Element* child = parent->GetChild(i);
  142. // If we've found our element, then break; the current index is our element's index.
  143. if (child == element)
  144. break;
  145. // Skip nodes that don't share our tag.
  146. if (child->GetTagName() != element->GetTagName())
  147. continue;
  148. element_index++;
  149. }
  150. return IsNth(selector.a, selector.b, element_index);
  151. }
  152. break;
  153. case StructuralSelectorType::Nth_Last_Of_Type:
  154. {
  155. Element* parent = element->GetParentNode();
  156. if (!parent)
  157. return false;
  158. // Start counting elements until we find this one.
  159. int element_index = 1;
  160. for (int i = parent->GetNumChildren() - 1; i >= 0; --i)
  161. {
  162. Element* child = parent->GetChild(i);
  163. // If we've found our element, then break; the current index is our element's index.
  164. if (child == element)
  165. break;
  166. // Skip nodes that don't share our tag.
  167. if (child->GetTagName() != element->GetTagName())
  168. continue;
  169. element_index++;
  170. }
  171. return IsNth(selector.a, selector.b, element_index);
  172. }
  173. break;
  174. case StructuralSelectorType::First_Child:
  175. {
  176. Element* parent = element->GetParentNode();
  177. if (!parent)
  178. return false;
  179. int child_index = 0;
  180. while (child_index < parent->GetNumChildren())
  181. {
  182. // If this child (the first non-text child) is our element, then the selector succeeds.
  183. Element* child = parent->GetChild(child_index);
  184. if (child == element)
  185. return true;
  186. // If this child is not a text element, then the selector fails; this element is non-trivial.
  187. if (!IsTextElement(child))
  188. return false;
  189. // Otherwise, skip over the text element to find the last non-trivial element.
  190. child_index++;
  191. }
  192. return false;
  193. }
  194. break;
  195. case StructuralSelectorType::Last_Child:
  196. {
  197. Element* parent = element->GetParentNode();
  198. if (!parent)
  199. return false;
  200. int child_index = parent->GetNumChildren() - 1;
  201. while (child_index >= 0)
  202. {
  203. // If this child (the last non-text child) is our element, then the selector succeeds.
  204. Element* child = parent->GetChild(child_index);
  205. if (child == element)
  206. return true;
  207. // If this child is not a text element, then the selector fails; this element is non-trivial.
  208. if (!IsTextElement(child))
  209. return false;
  210. // Otherwise, skip over the text element to find the last non-trivial element.
  211. child_index--;
  212. }
  213. return false;
  214. }
  215. break;
  216. case StructuralSelectorType::First_Of_Type:
  217. {
  218. Element* parent = element->GetParentNode();
  219. if (!parent)
  220. return false;
  221. int child_index = 0;
  222. while (child_index < parent->GetNumChildren())
  223. {
  224. // If this child is our element, then it's the first one we've found with our tag; the selector succeeds.
  225. Element* child = parent->GetChild(child_index);
  226. if (child == element)
  227. return true;
  228. // Otherwise, if this child shares our element's tag, then our element is not the first tagged child; the selector fails.
  229. if (child->GetTagName() == element->GetTagName())
  230. return false;
  231. child_index++;
  232. }
  233. return false;
  234. }
  235. break;
  236. case StructuralSelectorType::Last_Of_Type:
  237. {
  238. Element* parent = element->GetParentNode();
  239. if (!parent)
  240. return false;
  241. int child_index = parent->GetNumChildren() - 1;
  242. while (child_index >= 0)
  243. {
  244. // If this child is our element, then it's the first one we've found with our tag; the selector succeeds.
  245. Element* child = parent->GetChild(child_index);
  246. if (child == element)
  247. return true;
  248. // Otherwise, if this child shares our element's tag, then our element is not the first tagged child; the selector fails.
  249. if (child->GetTagName() == element->GetTagName())
  250. return false;
  251. child_index--;
  252. }
  253. return false;
  254. }
  255. break;
  256. case StructuralSelectorType::Only_Child:
  257. {
  258. Element* parent = element->GetParentNode();
  259. if (!parent)
  260. return false;
  261. const int num_children = parent->GetNumChildren();
  262. for (int i = 0; i < num_children; i++)
  263. {
  264. Element* child = parent->GetChild(i);
  265. // Skip the child if it is our element.
  266. if (child == element)
  267. continue;
  268. // Skip the child if it is trivial.
  269. if (IsTextElement(child))
  270. continue;
  271. return false;
  272. }
  273. return true;
  274. }
  275. break;
  276. case StructuralSelectorType::Only_Of_Type:
  277. {
  278. Element* parent = element->GetParentNode();
  279. if (!parent)
  280. return false;
  281. const int num_children = parent->GetNumChildren();
  282. for (int i = 0; i < num_children; i++)
  283. {
  284. Element* child = parent->GetChild(i);
  285. // Skip the child if it is our element.
  286. if (child == element)
  287. continue;
  288. // Skip the child if it does not share our tag.
  289. if (child->GetTagName() != element->GetTagName())
  290. continue;
  291. // We've found a similarly-tagged child to our element; selector fails.
  292. return false;
  293. }
  294. return true;
  295. }
  296. break;
  297. case StructuralSelectorType::Empty:
  298. {
  299. return element->GetNumChildren() == 0;
  300. }
  301. break;
  302. case StructuralSelectorType::Not:
  303. {
  304. if (!selector.selector_tree)
  305. {
  306. RMLUI_ERROR;
  307. return false;
  308. }
  309. bool inner_selector_matches = false;
  310. for (const StyleSheetNode* node : selector.selector_tree->leafs)
  311. {
  312. if (node->IsApplicable(element, scope))
  313. {
  314. inner_selector_matches = true;
  315. break;
  316. }
  317. }
  318. return !inner_selector_matches;
  319. }
  320. break;
  321. case StructuralSelectorType::Scope:
  322. {
  323. return scope && element == scope;
  324. }
  325. break;
  326. case StructuralSelectorType::Invalid:
  327. {
  328. RMLUI_ERROR;
  329. }
  330. break;
  331. }
  332. return false;
  333. }
  334. } // namespace Rml