Selectors.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 Selector {
  70. String selector;
  71. String expected_ids;
  72. };
  73. static const Vector<Selector> 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. // Recursively iterate through 'element' and all of its descendants to find all
  105. // elements matching a particular property used to tag matching selectors.
  106. static void GetMatchingIds(String& matching_ids, Element* element)
  107. {
  108. String id = element->GetId();
  109. if (!id.empty() && element->GetProperty<int>("drag") == (int)Style::Drag::Drag)
  110. {
  111. matching_ids += id + ' ';
  112. }
  113. for (int i = 0; i < element->GetNumChildren(); i++)
  114. {
  115. GetMatchingIds(matching_ids, element->GetChild(i));
  116. }
  117. }
  118. // Return the list of IDs that should match the above 'selectors.expected_ids'.
  119. static String ElementListToIds(const ElementList& elements)
  120. {
  121. String result;
  122. for (Element* element : elements)
  123. {
  124. result += element->GetId() + ' ';
  125. }
  126. if (!result.empty())
  127. result.pop_back();
  128. return result;
  129. }
  130. TEST_CASE("Selectors")
  131. {
  132. const Vector2i window_size(1024, 768);
  133. TestsSystemInterface system_interface;
  134. TestsRenderInterface render_interface;
  135. SetRenderInterface(&render_interface);
  136. SetSystemInterface(&system_interface);
  137. Initialise();
  138. Context* context = Rml::CreateContext("main", window_size);
  139. REQUIRE(context);
  140. SUBCASE("RCSS document selectors")
  141. {
  142. for (const Selector& selector : selectors)
  143. {
  144. const String selector_css = selector.selector + " { drag: drag; } ";
  145. const String document_string = doc_begin + selector_css + doc_end;
  146. ElementDocument* document = context->LoadDocumentFromMemory(document_string);
  147. REQUIRE(document);
  148. String matching_ids;
  149. GetMatchingIds(matching_ids, document);
  150. if (!matching_ids.empty())
  151. matching_ids.pop_back();
  152. CHECK_MESSAGE(matching_ids == selector.expected_ids, "Selector: " << selector.selector);
  153. context->UnloadDocument(document);
  154. }
  155. }
  156. SUBCASE("QuerySelector(All)")
  157. {
  158. const String document_string = doc_begin + doc_end;
  159. ElementDocument* document = context->LoadDocumentFromMemory(document_string);
  160. REQUIRE(document);
  161. for (const Selector& selector : selectors)
  162. {
  163. ElementList elements;
  164. document->QuerySelectorAll(elements, selector.selector);
  165. String matching_ids = ElementListToIds(elements);
  166. Element* first_element = document->QuerySelector(selector.selector);
  167. if (first_element)
  168. {
  169. CHECK_MESSAGE(first_element == elements[0], "QuerySelector does not return the first match of QuerySelectorAll.");
  170. }
  171. else
  172. {
  173. CHECK_MESSAGE(elements.empty(), "QuerySelector found nothing, while QuerySelectorAll found " << elements.size() << " element(s).");
  174. }
  175. CHECK_MESSAGE(matching_ids == selector.expected_ids, "QuerySelector: " << selector.selector);
  176. context->UnloadDocument(document);
  177. }
  178. }
  179. Rml::Shutdown();
  180. }