Selectors.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 "../Common/TestsInterface.h"
  29. #include <RmlUi/Core/Context.h>
  30. #include <RmlUi/Core/Core.h>
  31. #include <RmlUi/Core/Element.h>
  32. #include <RmlUi/Core/ElementDocument.h>
  33. #include <RmlUi/Core/Types.h>
  34. #include <doctest.h>
  35. using namespace Rml;
  36. // clang-format off
  37. static const String doc_begin = R"(
  38. <rml>
  39. <head>
  40. <title>Demo</title>
  41. <style>
  42. body
  43. {
  44. width: 400px;
  45. height: 300px;
  46. margin: auto;
  47. }
  48. )";
  49. static const String doc_end = R"(
  50. </style>
  51. </head>
  52. <body>
  53. <div id="X" class="hello"/>
  54. <span id="Y" class="world"/>
  55. <div id="Z" class="hello world"/>
  56. <div id="P" class="parent">
  57. <h1 id="A"/>
  58. <p id="B"/>
  59. <p id="C"/>
  60. <p id="D"> <span id="D0"/><span id="D1"/> </p>
  61. <h3 id="E"/>
  62. <p id="F"> <span id="F0"/> </p>
  63. <p id="G"/>
  64. <p id="H" class="hello"/>
  65. </div>
  66. <input id="I" type="checkbox" checked/>
  67. </body>
  68. </rml>
  69. )";
  70. enum class SelectorOp { None, RemoveElementsByIds, InsertElementBefore, RemoveClasses, RemoveChecked };
  71. struct QuerySelector {
  72. QuerySelector(String selector, String expected_ids) : selector(std::move(selector)), expected_ids(std::move(expected_ids)) {}
  73. QuerySelector(String selector, String expected_ids, SelectorOp operation, String operation_argument, String expected_ids_after_operation) :
  74. selector(std::move(selector)), expected_ids(std::move(expected_ids)), operation(operation), operation_argument(std::move(operation_argument)),
  75. expected_ids_after_operation(std::move(expected_ids_after_operation))
  76. {}
  77. String selector;
  78. String expected_ids;
  79. // Optionally also test the selector after dynamically making a structural operation on the document.
  80. SelectorOp operation = SelectorOp::None;
  81. String operation_argument;
  82. String expected_ids_after_operation;
  83. };
  84. static const Vector<QuerySelector> query_selectors =
  85. {
  86. { "span", "Y D0 D1 F0" },
  87. { ".hello", "X Z H" },
  88. { ".hello.world", "Z" },
  89. { "div.hello", "X Z" },
  90. { "body .hello", "X Z H" },
  91. { "body>.hello", "X Z" },
  92. { "body > .hello", "X Z" },
  93. { ".parent *", "A B C D D0 D1 E F F0 G H" },
  94. { ".parent > *", "A B C D E F G H" },
  95. { ":checked", "I", SelectorOp::RemoveChecked, "I", "" },
  96. { ".parent :nth-child(odd)", "A C D0 E F0 G" },
  97. { ".parent > :nth-child(even)", "B D F H", SelectorOp::RemoveClasses, "parent", "" },
  98. { ":first-child", "X A D0 F0", SelectorOp::RemoveElementsByIds, "A F0", "X B D0" },
  99. { ":last-child", "D1 F0 H I", SelectorOp::RemoveElementsByIds, "D0 H", "D1 F0 G I" },
  100. { "h1:nth-child(2)", "", SelectorOp::InsertElementBefore, "A", "A" },
  101. { "p:nth-child(2)", "B", SelectorOp::InsertElementBefore, "A", "" },
  102. { "p:nth-child(2)", "B", SelectorOp::RemoveElementsByIds, "A", "C" },
  103. { "p:nth-child(3n+1)", "D G", SelectorOp::RemoveElementsByIds, "B", "H" },
  104. { "p:nth-child(3n + 1)", "D G" },
  105. { "#P > :nth-last-child(2n+1)", "B D F H" },
  106. { "#P p:nth-of-type(odd)", "B D G" },
  107. { "span:first-child", "D0 F0" },
  108. { "body span:first-child", "D0 F0" },
  109. { "body > p span:first-child", "" },
  110. { "body > div span:first-child", "D0 F0" },
  111. { ":nth-child(4) * span:first-child", "D0 F0", SelectorOp::RemoveElementsByIds, "X", "" },
  112. { "p:nth-last-of-type(3n+1)", "D H" },
  113. { ":first-of-type", "X Y A B D0 E F0 I" },
  114. { ":last-of-type", "Y P A D1 E F0 H I" },
  115. { ":only-child", "F0", SelectorOp::RemoveElementsByIds, "D0", "D1 F0" },
  116. { ":only-of-type", "Y A E F0 I" },
  117. { "span:empty", "Y D0 D1 F0" },
  118. { ".hello.world, #P span, #I", "Z D0 D1 F0 I", SelectorOp::RemoveClasses, "world", "D0 D1 F0 I" },
  119. { "body * span", "D0 D1 F0" },
  120. { "#E + #F", "F", SelectorOp::InsertElementBefore, "F", "" },
  121. { "#E+#F", "F" },
  122. { "#E +#F", "F" },
  123. { "#E+ #F", "F" },
  124. { "#F + #E", "" },
  125. { "div.parent > #B + p", "C" },
  126. { "div.parent > #B + div", "" },
  127. { "#B ~ #F", "F" },
  128. { "#B~#F", "F" },
  129. { "#B ~#F", "F" },
  130. { "#B~ #F", "F" },
  131. { "#F ~ #B", "" },
  132. { "div.parent > #B ~ p:empty", "C G H", SelectorOp::InsertElementBefore, "H", "C G Inserted H" },
  133. { "div.parent > #B ~ * span", "D0 D1 F0" },
  134. };
  135. struct ClosestSelector {
  136. String start_id;
  137. String selector;
  138. String expected_id;
  139. };
  140. static const Vector<ClosestSelector> closest_selectors =
  141. {
  142. { "D1", "#P", "P" },
  143. { "D1", "#P, body", "P" },
  144. { "D1", "#P, #D", "D" },
  145. { "D1", "#Z", "" },
  146. { "D1", "#D1", "" },
  147. { "D1", "#D0", "" },
  148. { "D1", "div", "P" },
  149. { "D1", "p", "D" },
  150. { "D1", ":nth-child(4)", "D" },
  151. { "D1", "div:nth-child(4)", "P" },
  152. };
  153. // clang-format on
  154. // Recursively iterate through 'element' and all of its descendants to find all
  155. // elements matching a particular property used to tag matching selectors.
  156. static void GetMatchingIds(String& matching_ids, Element* element)
  157. {
  158. String id = element->GetId();
  159. if (!id.empty() && element->GetProperty<int>("drag") == (int)Style::Drag::Drag)
  160. {
  161. if (!matching_ids.empty())
  162. matching_ids += ' ';
  163. matching_ids += id;
  164. }
  165. for (int i = 0; i < element->GetNumChildren(); i++)
  166. {
  167. GetMatchingIds(matching_ids, element->GetChild(i));
  168. }
  169. }
  170. // Return the list of IDs that should match the above 'selectors.expected_ids'.
  171. static String ElementListToIds(const ElementList& elements)
  172. {
  173. String result;
  174. for (Element* element : elements)
  175. {
  176. result += element->GetId() + ' ';
  177. }
  178. if (!result.empty())
  179. result.pop_back();
  180. return result;
  181. }
  182. static void RemoveElementsWithIds(ElementDocument* document, const String& remove_ids)
  183. {
  184. StringList remove_id_list;
  185. StringUtilities::ExpandString(remove_id_list, remove_ids, ' ');
  186. for (const String& id : remove_id_list)
  187. {
  188. if (Element* element = document->GetElementById(id))
  189. element->GetParentNode()->RemoveChild(element);
  190. }
  191. }
  192. static void RemoveClassesFromAllElements(ElementDocument* document, const String& remove_classes)
  193. {
  194. StringList class_list;
  195. StringUtilities::ExpandString(class_list, remove_classes, ' ');
  196. for (const String& name : class_list)
  197. {
  198. ElementList element_list;
  199. document->GetElementsByClassName(element_list, name);
  200. for (Element* element : element_list)
  201. element->SetClass(name, false);
  202. }
  203. }
  204. static void InsertElementBefore(ElementDocument* document, const String& before_id)
  205. {
  206. Element* element = document->GetElementById(before_id);
  207. ElementPtr new_element = document->CreateElement("p");
  208. new_element->SetId("Inserted");
  209. element->GetParentNode()->InsertBefore(std::move(new_element), element);
  210. }
  211. TEST_CASE("Selectors")
  212. {
  213. const Vector2i window_size(1024, 768);
  214. TestsSystemInterface system_interface;
  215. TestsRenderInterface render_interface;
  216. SetRenderInterface(&render_interface);
  217. SetSystemInterface(&system_interface);
  218. Initialise();
  219. Context* context = Rml::CreateContext("main", window_size);
  220. REQUIRE(context);
  221. SUBCASE("RCSS document selectors")
  222. {
  223. for (const QuerySelector& selector : query_selectors)
  224. {
  225. const String selector_css = selector.selector + " { drag: drag; } ";
  226. const String document_string = doc_begin + selector_css + doc_end;
  227. ElementDocument* document = context->LoadDocumentFromMemory(document_string);
  228. REQUIRE(document);
  229. String matching_ids;
  230. GetMatchingIds(matching_ids, document);
  231. CHECK_MESSAGE(matching_ids == selector.expected_ids, "Selector: " << selector.selector);
  232. // Also check validity of selectors after structural document changes.
  233. if (selector.operation != SelectorOp::None)
  234. {
  235. String operation_str;
  236. switch (selector.operation)
  237. {
  238. case SelectorOp::RemoveElementsByIds:
  239. RemoveElementsWithIds(document, selector.operation_argument);
  240. operation_str = "RemoveElementsByIds";
  241. break;
  242. case SelectorOp::InsertElementBefore:
  243. InsertElementBefore(document, selector.operation_argument);
  244. operation_str = "InsertElementBefore";
  245. break;
  246. case SelectorOp::RemoveClasses:
  247. RemoveClassesFromAllElements(document, selector.operation_argument);
  248. operation_str = "RemoveClasses";
  249. break;
  250. case SelectorOp::RemoveChecked:
  251. document->GetElementById(selector.operation_argument)->RemoveAttribute("checked");
  252. operation_str = "RemoveChecked";
  253. break;
  254. case SelectorOp::None:
  255. break;
  256. }
  257. context->Update();
  258. String matching_ids_after_operation;
  259. GetMatchingIds(matching_ids_after_operation, document);
  260. CHECK_MESSAGE(matching_ids_after_operation == selector.expected_ids_after_operation, "Selector: ", selector.selector,
  261. " Operation: ", operation_str, " Argument: ", selector.operation_argument);
  262. }
  263. context->UnloadDocument(document);
  264. }
  265. }
  266. SUBCASE("QuerySelector(All)")
  267. {
  268. const String document_string = doc_begin + doc_end;
  269. ElementDocument* document = context->LoadDocumentFromMemory(document_string);
  270. REQUIRE(document);
  271. for (const QuerySelector& selector : query_selectors)
  272. {
  273. ElementList elements;
  274. document->QuerySelectorAll(elements, selector.selector);
  275. String matching_ids = ElementListToIds(elements);
  276. Element* first_element = document->QuerySelector(selector.selector);
  277. if (first_element)
  278. {
  279. CHECK_MESSAGE(first_element == elements[0], "QuerySelector does not return the first match of QuerySelectorAll.");
  280. }
  281. else
  282. {
  283. CHECK_MESSAGE(elements.empty(), "QuerySelector found nothing, while QuerySelectorAll found " << elements.size() << " element(s).");
  284. }
  285. CHECK_MESSAGE(matching_ids == selector.expected_ids, "QuerySelector: " << selector.selector);
  286. }
  287. context->UnloadDocument(document);
  288. }
  289. SUBCASE("Closest")
  290. {
  291. const String document_string = doc_begin + doc_end;
  292. ElementDocument* document = context->LoadDocumentFromMemory(document_string);
  293. REQUIRE(document);
  294. for (const ClosestSelector& selector : closest_selectors)
  295. {
  296. Element* start = document->GetElementById(selector.start_id);
  297. REQUIRE(start);
  298. Element* match = start->Closest(selector.selector);
  299. const String match_id = match ? match->GetId() : "";
  300. CHECK_MESSAGE(match_id == selector.expected_id, "Closest() selector '" << selector.selector << "' from " << selector.start_id);
  301. }
  302. context->UnloadDocument(document);
  303. }
  304. Rml::Shutdown();
  305. }