Selectors.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. 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. }
  47. )";
  48. static const String doc_end = R"(
  49. </style>
  50. </head>
  51. <body>
  52. <div id="X" class="hello"/>
  53. <span id="Y" class="world"/>
  54. <div id="Z" class="hello world"/>
  55. <div id="P" class="parent">
  56. <h1 id="A"/>
  57. <p id="B"/>
  58. <p id="C"/>
  59. <p id="D"> <span id="D0"/><span id="D1"/> </p>
  60. <h3 id="E"/>
  61. <p id="F"> <span id="F0"/> </p>
  62. <p id="G"/>
  63. <p id="H" class="hello"/>
  64. </div>
  65. <input id="I" type="checkbox" checked/>
  66. </body>
  67. </rml>
  68. )";
  69. struct QuerySelector {
  70. String selector;
  71. String expected_ids;
  72. };
  73. static const Vector<QuerySelector> query_selectors =
  74. {
  75. { "span", "Y D0 D1 F0" },
  76. { ".hello", "X Z H" },
  77. { ".hello.world", "Z" },
  78. { "div.hello", "X Z" },
  79. { "body .hello", "X Z H" },
  80. { "body>.hello", "X Z" },
  81. { "body > .hello", "X Z" },
  82. { ".parent *", "A B C D D0 D1 E F F0 G H" },
  83. { ".parent > *", "A B C D E F G H" },
  84. { ":checked", "I" },
  85. { ".parent :nth-child(odd)", "A C D0 E F0 G" },
  86. { ".parent > :nth-child(even)", "B D F H" },
  87. { ":first-child", "X A D0 F0" },
  88. { ":last-child", "D1 F0 H I" },
  89. { "p:nth-child(2)", "B" },
  90. { "h1:nth-child(2)", "" },
  91. { "p:nth-child(3n+1)", "D G" },
  92. { "p:nth-child(3n + 1)", "D G" },
  93. { "#P > :nth-last-child(2n+1)", "B D F H" },
  94. { "#P p:nth-of-type(odd)", "B D G" },
  95. { "p:nth-last-of-type(3n+1)", "D H" },
  96. { ":first-of-type", "X Y A B D0 E F0 I" },
  97. { ":last-of-type", "Y P A D1 E F0 H I" },
  98. { ":only-child", "F0" },
  99. { ":only-of-type", "Y A E F0 I" },
  100. { "span:empty", "Y D0 D1 F0" },
  101. { ".hello.world, #P span, #I", "Z D0 D1 F0 I" },
  102. { "body * span", "D0 D1 F0" },
  103. };
  104. struct ClosestSelector {
  105. String start_id;
  106. String selector;
  107. String expected_id;
  108. };
  109. static const Vector<ClosestSelector> closest_selectors =
  110. {
  111. { "D1", "#P", "P" },
  112. { "D1", "#P, body", "P" },
  113. { "D1", "#P, #D", "D" },
  114. { "D1", "#Z", "" },
  115. { "D1", "#D1", "" },
  116. { "D1", "#D0", "" },
  117. { "D1", "div", "P" },
  118. { "D1", "p", "D" },
  119. { "D1", ":nth-child(4)", "D" },
  120. { "D1", "div:nth-child(4)", "P" },
  121. };
  122. // Recursively iterate through 'element' and all of its descendants to find all
  123. // elements matching a particular property used to tag matching selectors.
  124. static void GetMatchingIds(String& matching_ids, Element* element)
  125. {
  126. String id = element->GetId();
  127. if (!id.empty() && element->GetProperty<int>("drag") == (int)Style::Drag::Drag)
  128. {
  129. matching_ids += id + ' ';
  130. }
  131. for (int i = 0; i < element->GetNumChildren(); i++)
  132. {
  133. GetMatchingIds(matching_ids, element->GetChild(i));
  134. }
  135. }
  136. // Return the list of IDs that should match the above 'selectors.expected_ids'.
  137. static String ElementListToIds(const ElementList& elements)
  138. {
  139. String result;
  140. for (Element* element : elements)
  141. {
  142. result += element->GetId() + ' ';
  143. }
  144. if (!result.empty())
  145. result.pop_back();
  146. return result;
  147. }
  148. TEST_CASE("Selectors")
  149. {
  150. const Vector2i window_size(1024, 768);
  151. TestsSystemInterface system_interface;
  152. TestsRenderInterface render_interface;
  153. SetRenderInterface(&render_interface);
  154. SetSystemInterface(&system_interface);
  155. Initialise();
  156. Context* context = Rml::CreateContext("main", window_size);
  157. REQUIRE(context);
  158. SUBCASE("RCSS document selectors")
  159. {
  160. for (const QuerySelector& selector : query_selectors)
  161. {
  162. const String selector_css = selector.selector + " { drag: drag; } ";
  163. const String document_string = doc_begin + selector_css + doc_end;
  164. ElementDocument* document = context->LoadDocumentFromMemory(document_string);
  165. REQUIRE(document);
  166. String matching_ids;
  167. GetMatchingIds(matching_ids, document);
  168. if (!matching_ids.empty())
  169. matching_ids.pop_back();
  170. CHECK_MESSAGE(matching_ids == selector.expected_ids, "Selector: " << selector.selector);
  171. context->UnloadDocument(document);
  172. }
  173. }
  174. SUBCASE("QuerySelector(All)")
  175. {
  176. const String document_string = doc_begin + doc_end;
  177. ElementDocument* document = context->LoadDocumentFromMemory(document_string);
  178. REQUIRE(document);
  179. for (const QuerySelector& selector : query_selectors)
  180. {
  181. ElementList elements;
  182. document->QuerySelectorAll(elements, selector.selector);
  183. String matching_ids = ElementListToIds(elements);
  184. Element* first_element = document->QuerySelector(selector.selector);
  185. if (first_element)
  186. {
  187. CHECK_MESSAGE(first_element == elements[0], "QuerySelector does not return the first match of QuerySelectorAll.");
  188. }
  189. else
  190. {
  191. CHECK_MESSAGE(elements.empty(), "QuerySelector found nothing, while QuerySelectorAll found " << elements.size() << " element(s).");
  192. }
  193. CHECK_MESSAGE(matching_ids == selector.expected_ids, "QuerySelector: " << selector.selector);
  194. }
  195. context->UnloadDocument(document);
  196. }
  197. SUBCASE("Closest")
  198. {
  199. const String document_string = doc_begin + doc_end;
  200. ElementDocument* document = context->LoadDocumentFromMemory(document_string);
  201. REQUIRE(document);
  202. for (const ClosestSelector& selector : closest_selectors)
  203. {
  204. Element* start = document->GetElementById(selector.start_id);
  205. REQUIRE(start);
  206. Element* match = start->Closest(selector.selector);
  207. const String match_id = match ? match->GetId() : "";
  208. CHECK_MESSAGE(match_id == selector.expected_id, "Closest() selector '" << selector.selector << "' from " << selector.start_id);
  209. }
  210. context->UnloadDocument(document);
  211. }
  212. Rml::Shutdown();
  213. }