StyleSheetSelector.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 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. namespace Rml {
  32. static inline bool IsTextElement(const Element* element)
  33. {
  34. return element->GetTagName() == "#text";
  35. }
  36. // Returns true if a positive integer can be found for n in the equation an + b = count.
  37. static bool IsNth(int a, int b, int count)
  38. {
  39. int x = count;
  40. x -= b;
  41. if (a != 0)
  42. x /= a;
  43. return (x >= 0 && x * a + b == count);
  44. }
  45. bool IsSelectorApplicable(const Element* element, const StructuralSelector& selector)
  46. {
  47. RMLUI_ASSERT(element);
  48. switch (selector.type)
  49. {
  50. case StructuralSelectorType::Nth_Child:
  51. {
  52. Element* parent = element->GetParentNode();
  53. if (!parent)
  54. return false;
  55. // Start counting elements until we find this one.
  56. int element_index = 1;
  57. for (int i = 0; i < parent->GetNumChildren(); i++)
  58. {
  59. Element* child = parent->GetChild(i);
  60. // Skip text nodes.
  61. if (IsTextElement(child))
  62. continue;
  63. // If we've found our element, then break; the current index is our element's index.
  64. if (child == element)
  65. break;
  66. element_index++;
  67. }
  68. return IsNth(selector.a, selector.b, element_index);
  69. }
  70. break;
  71. case StructuralSelectorType::Nth_Last_Child:
  72. {
  73. Element* parent = element->GetParentNode();
  74. if (!parent)
  75. return false;
  76. // Start counting elements until we find this one.
  77. int element_index = 1;
  78. for (int i = parent->GetNumChildren() - 1; i >= 0; --i)
  79. {
  80. Element* child = parent->GetChild(i);
  81. // Skip text nodes.
  82. if (IsTextElement(child))
  83. continue;
  84. // If we've found our element, then break; the current index is our element's index.
  85. if (child == element)
  86. break;
  87. element_index++;
  88. }
  89. return IsNth(selector.a, selector.b, element_index);
  90. }
  91. break;
  92. case StructuralSelectorType::Nth_Of_Type:
  93. {
  94. Element* parent = element->GetParentNode();
  95. if (!parent)
  96. return false;
  97. // Start counting elements until we find this one.
  98. int element_index = 1;
  99. const int num_children = parent->GetNumChildren();
  100. for (int i = 0; i < num_children; i++)
  101. {
  102. Element* child = parent->GetChild(i);
  103. // If we've found our element, then break; the current index is our element's index.
  104. if (child == element)
  105. break;
  106. // Skip nodes that don't share our tag.
  107. if (child->GetTagName() != element->GetTagName())
  108. continue;
  109. element_index++;
  110. }
  111. return IsNth(selector.a, selector.b, element_index);
  112. }
  113. break;
  114. case StructuralSelectorType::Nth_Last_Of_Type:
  115. {
  116. Element* parent = element->GetParentNode();
  117. if (!parent)
  118. return false;
  119. // Start counting elements until we find this one.
  120. int element_index = 1;
  121. for (int i = parent->GetNumChildren() - 1; i >= 0; --i)
  122. {
  123. Element* child = parent->GetChild(i);
  124. // If we've found our element, then break; the current index is our element's index.
  125. if (child == element)
  126. break;
  127. // Skip nodes that don't share our tag.
  128. if (child->GetTagName() != element->GetTagName())
  129. continue;
  130. element_index++;
  131. }
  132. return IsNth(selector.a, selector.b, element_index);
  133. }
  134. break;
  135. case StructuralSelectorType::First_Child:
  136. {
  137. Element* parent = element->GetParentNode();
  138. if (!parent)
  139. return false;
  140. int child_index = 0;
  141. while (child_index < parent->GetNumChildren())
  142. {
  143. // If this child (the first non-text child) is our element, then the selector succeeds.
  144. Element* child = parent->GetChild(child_index);
  145. if (child == element)
  146. return true;
  147. // If this child is not a text element, then the selector fails; this element is non-trivial.
  148. if (!IsTextElement(child))
  149. return false;
  150. // Otherwise, skip over the text element to find the last non-trivial element.
  151. child_index++;
  152. }
  153. return false;
  154. }
  155. break;
  156. case StructuralSelectorType::Last_Child:
  157. {
  158. Element* parent = element->GetParentNode();
  159. if (!parent)
  160. return false;
  161. int child_index = parent->GetNumChildren() - 1;
  162. while (child_index >= 0)
  163. {
  164. // If this child (the last non-text child) is our element, then the selector succeeds.
  165. Element* child = parent->GetChild(child_index);
  166. if (child == element)
  167. return true;
  168. // If this child is not a text element, then the selector fails; this element is non-trivial.
  169. if (!IsTextElement(child))
  170. return false;
  171. // Otherwise, skip over the text element to find the last non-trivial element.
  172. child_index--;
  173. }
  174. return false;
  175. }
  176. break;
  177. case StructuralSelectorType::First_Of_Type:
  178. {
  179. Element* parent = element->GetParentNode();
  180. if (!parent)
  181. return false;
  182. int child_index = 0;
  183. while (child_index < parent->GetNumChildren())
  184. {
  185. // If this child is our element, then it's the first one we've found with our tag; the selector succeeds.
  186. Element* child = parent->GetChild(child_index);
  187. if (child == element)
  188. return true;
  189. // Otherwise, if this child shares our element's tag, then our element is not the first tagged child; the selector fails.
  190. if (child->GetTagName() == element->GetTagName())
  191. return false;
  192. child_index++;
  193. }
  194. return false;
  195. }
  196. break;
  197. case StructuralSelectorType::Last_Of_Type:
  198. {
  199. Element* parent = element->GetParentNode();
  200. if (!parent)
  201. return false;
  202. int child_index = parent->GetNumChildren() - 1;
  203. while (child_index >= 0)
  204. {
  205. // If this child is our element, then it's the first one we've found with our tag; the selector succeeds.
  206. Element* child = parent->GetChild(child_index);
  207. if (child == element)
  208. return true;
  209. // Otherwise, if this child shares our element's tag, then our element is not the first tagged child; the selector fails.
  210. if (child->GetTagName() == element->GetTagName())
  211. return false;
  212. child_index--;
  213. }
  214. return false;
  215. }
  216. break;
  217. case StructuralSelectorType::Only_Child:
  218. {
  219. Element* parent = element->GetParentNode();
  220. if (!parent)
  221. return false;
  222. const int num_children = parent->GetNumChildren();
  223. for (int i = 0; i < num_children; i++)
  224. {
  225. Element* child = parent->GetChild(i);
  226. // Skip the child if it is our element.
  227. if (child == element)
  228. continue;
  229. // Skip the child if it is trivial.
  230. if (IsTextElement(child))
  231. continue;
  232. return false;
  233. }
  234. return true;
  235. }
  236. break;
  237. case StructuralSelectorType::Only_Of_Type:
  238. {
  239. Element* parent = element->GetParentNode();
  240. if (!parent)
  241. return false;
  242. const int num_children = parent->GetNumChildren();
  243. for (int i = 0; i < num_children; i++)
  244. {
  245. Element* child = parent->GetChild(i);
  246. // Skip the child if it is our element.
  247. if (child == element)
  248. continue;
  249. // Skip the child if it does not share our tag.
  250. if (child->GetTagName() != element->GetTagName())
  251. continue;
  252. // We've found a similarly-tagged child to our element; selector fails.
  253. return false;
  254. }
  255. return true;
  256. }
  257. break;
  258. case StructuralSelectorType::Empty:
  259. {
  260. return element->GetNumChildren() == 0;
  261. }
  262. break;
  263. case StructuralSelectorType::Not:
  264. {
  265. if (!selector.selector_tree)
  266. {
  267. RMLUI_ERROR;
  268. return false;
  269. }
  270. bool inner_selector_matches = false;
  271. for (const StyleSheetNode* node : selector.selector_tree->leafs)
  272. {
  273. if (node->IsApplicable(element))
  274. {
  275. inner_selector_matches = true;
  276. break;
  277. }
  278. }
  279. return !inner_selector_matches;
  280. }
  281. break;
  282. case StructuralSelectorType::Invalid:
  283. {
  284. RMLUI_ERROR;
  285. }
  286. break;
  287. }
  288. return false;
  289. }
  290. } // namespace Rml