Selectors.cpp 12 KB

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