Selectors.cpp 14 KB

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