Selectors.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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, RemoveId, RemoveChecked, SetHover };
  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. { "#A + #B", "B", SelectorOp::RemoveId, "A", "" },
  129. { "* + #A", "" },
  130. { "#H + *", "" },
  131. { "#P + *", "I" },
  132. { "div.parent > #B + p", "C" },
  133. { "div.parent > #B + div", "" },
  134. { "#B ~ #F", "F" },
  135. { "#B~#F", "F" },
  136. { "#B ~#F", "F" },
  137. { "#B~ #F", "F" },
  138. { "#F ~ #B", "" },
  139. { "div.parent > #B ~ p:empty", "C G H", SelectorOp::InsertElementBefore, "H", "C G Inserted H" },
  140. { "div.parent > #B ~ * span", "D0 D1 F0" },
  141. { ":not(*)", "" },
  142. { ":not(span)", "X Z P A B C D E F G H I" },
  143. { "#D :not(#D0)", "D1" },
  144. { "body > :not(:checked)", "X Y Z P", SelectorOp::RemoveChecked, "I", "X Y Z P I" },
  145. { "div.hello:not(.world)", "X" },
  146. { ":not(div,:nth-child(2),p *)", "A C D E F G H I" },
  147. { ".hello + .world", "Y", SelectorOp::RemoveClasses, "hello", "" },
  148. { "#B ~ h3", "E", SelectorOp::RemoveId, "B", "" },
  149. { "#Z + div > :nth-child(2)", "B", SelectorOp::RemoveId, "Z", "" },
  150. { ":hover + #P #D1", "", SelectorOp::SetHover, "Z", "D1" },
  151. { ":not(:hover) + #P #D1", "D1", SelectorOp::SetHover, "Z", "" },
  152. { "#X + #Y", "Y", SelectorOp::RemoveId, "X", "" },
  153. { "body > * #D0", "D0" },
  154. { "#E + * ~ *", "G H" },
  155. { "#B + * ~ #G", "G" },
  156. { "body > :nth-child(4) span:first-child", "D0 F0", SelectorOp::RemoveElementsByIds, "X", "" },
  157. };
  158. struct ClosestSelector {
  159. String start_id;
  160. String selector;
  161. String expected_id;
  162. };
  163. static const Vector<ClosestSelector> closest_selectors =
  164. {
  165. { "D1", "#P", "P" },
  166. { "D1", "#P, body", "P" },
  167. { "D1", "#P, #D", "D" },
  168. { "D1", "#Z", "" },
  169. { "D1", "#D1", "" },
  170. { "D1", "#D0", "" },
  171. { "D1", "div", "P" },
  172. { "D1", "p", "D" },
  173. { "D1", ":nth-child(4)", "D" },
  174. { "D1", "div:nth-child(4)", "P" },
  175. };
  176. // clang-format on
  177. // Recursively iterate through 'element' and all of its descendants to find all
  178. // elements matching a particular property used to tag matching selectors.
  179. static void GetMatchingIds(String& matching_ids, Element* element)
  180. {
  181. String id = element->GetId();
  182. if (!id.empty() && element->GetProperty<int>("drag") == (int)Style::Drag::Drag)
  183. {
  184. if (!matching_ids.empty())
  185. matching_ids += ' ';
  186. matching_ids += id;
  187. }
  188. for (int i = 0; i < element->GetNumChildren(); i++)
  189. {
  190. GetMatchingIds(matching_ids, element->GetChild(i));
  191. }
  192. }
  193. // Return the list of IDs that should match the above 'selectors.expected_ids'.
  194. static String ElementListToIds(const ElementList& elements)
  195. {
  196. String result;
  197. for (Element* element : elements)
  198. {
  199. result += element->GetId() + ' ';
  200. }
  201. if (!result.empty())
  202. result.pop_back();
  203. return result;
  204. }
  205. static void RemoveElementsWithIds(ElementDocument* document, const String& remove_ids)
  206. {
  207. StringList remove_id_list;
  208. StringUtilities::ExpandString(remove_id_list, remove_ids, ' ');
  209. for (const String& id : remove_id_list)
  210. {
  211. if (Element* element = document->GetElementById(id))
  212. element->GetParentNode()->RemoveChild(element);
  213. }
  214. }
  215. static void RemoveClassesFromAllElements(ElementDocument* document, const String& remove_classes)
  216. {
  217. StringList class_list;
  218. StringUtilities::ExpandString(class_list, remove_classes, ' ');
  219. for (const String& name : class_list)
  220. {
  221. ElementList element_list;
  222. document->GetElementsByClassName(element_list, name);
  223. for (Element* element : element_list)
  224. element->SetClass(name, false);
  225. }
  226. }
  227. static void InsertElementBefore(ElementDocument* document, const String& before_id)
  228. {
  229. Element* element = document->GetElementById(before_id);
  230. ElementPtr new_element = document->CreateElement("p");
  231. new_element->SetId("Inserted");
  232. element->GetParentNode()->InsertBefore(std::move(new_element), element);
  233. }
  234. TEST_CASE("Selectors")
  235. {
  236. Context* context = TestsShell::GetContext();
  237. SUBCASE("RCSS document selectors")
  238. {
  239. for (const QuerySelector& selector : query_selectors)
  240. {
  241. const String selector_css = selector.selector + " { drag: drag; } ";
  242. const String document_string = doc_begin + selector_css + doc_end;
  243. ElementDocument* document = context->LoadDocumentFromMemory(document_string);
  244. // Update the context to settle any dirty state.
  245. context->Update();
  246. String matching_ids;
  247. GetMatchingIds(matching_ids, document);
  248. CHECK_MESSAGE(matching_ids == selector.expected_ids, "Selector: " << selector.selector);
  249. // Also check validity of selectors after structural document changes.
  250. if (selector.operation != SelectorOp::None)
  251. {
  252. String operation_str;
  253. switch (selector.operation)
  254. {
  255. case SelectorOp::RemoveElementsByIds:
  256. RemoveElementsWithIds(document, selector.operation_argument);
  257. operation_str = "RemoveElementsByIds";
  258. break;
  259. case SelectorOp::InsertElementBefore:
  260. InsertElementBefore(document, selector.operation_argument);
  261. operation_str = "InsertElementBefore";
  262. break;
  263. case SelectorOp::RemoveClasses:
  264. RemoveClassesFromAllElements(document, selector.operation_argument);
  265. operation_str = "RemoveClasses";
  266. break;
  267. case SelectorOp::RemoveChecked:
  268. document->GetElementById(selector.operation_argument)->RemoveAttribute("checked");
  269. operation_str = "RemoveChecked";
  270. break;
  271. case SelectorOp::RemoveId:
  272. document->GetElementById(selector.operation_argument)->SetId("");
  273. operation_str = "RemoveId";
  274. break;
  275. case SelectorOp::SetHover:
  276. document->GetElementById(selector.operation_argument)->SetPseudoClass("hover", true);
  277. operation_str = "SetHover";
  278. break;
  279. case SelectorOp::None:
  280. break;
  281. }
  282. context->Update();
  283. String matching_ids_after_operation;
  284. GetMatchingIds(matching_ids_after_operation, document);
  285. CHECK_MESSAGE(matching_ids_after_operation == selector.expected_ids_after_operation, "Selector: ", selector.selector,
  286. " Operation: ", operation_str, " Argument: ", selector.operation_argument);
  287. }
  288. context->UnloadDocument(document);
  289. }
  290. }
  291. SUBCASE("QuerySelector(All)")
  292. {
  293. const String document_string = doc_begin + doc_end;
  294. ElementDocument* document = context->LoadDocumentFromMemory(document_string);
  295. REQUIRE(document);
  296. for (const QuerySelector& selector : query_selectors)
  297. {
  298. ElementList elements;
  299. document->QuerySelectorAll(elements, selector.selector);
  300. String matching_ids = ElementListToIds(elements);
  301. Element* first_element = document->QuerySelector(selector.selector);
  302. if (first_element)
  303. {
  304. CHECK_MESSAGE(first_element == elements[0], "QuerySelector does not return the first match of QuerySelectorAll.");
  305. }
  306. else
  307. {
  308. CHECK_MESSAGE(elements.empty(), "QuerySelector found nothing, while QuerySelectorAll found " << elements.size() << " element(s).");
  309. }
  310. CHECK_MESSAGE(matching_ids == selector.expected_ids, "QuerySelector: " << selector.selector);
  311. }
  312. context->UnloadDocument(document);
  313. }
  314. SUBCASE("Closest")
  315. {
  316. const String document_string = doc_begin + doc_end;
  317. ElementDocument* document = context->LoadDocumentFromMemory(document_string);
  318. REQUIRE(document);
  319. for (const ClosestSelector& selector : closest_selectors)
  320. {
  321. Element* start = document->GetElementById(selector.start_id);
  322. REQUIRE(start);
  323. Element* match = start->Closest(selector.selector);
  324. const String match_id = match ? match->GetId() : "";
  325. CHECK_MESSAGE(match_id == selector.expected_id, "Closest() selector '" << selector.selector << "' from " << selector.start_id);
  326. }
  327. context->UnloadDocument(document);
  328. }
  329. TestsShell::ShutdownShell();
  330. }