Selectors.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "TestsInterface.h"
  29. #include <RmlUi/Core.h>
  30. #include <RmlUi/Debugger.h>
  31. #include <doctest.h>
  32. using namespace Rml;
  33. static const String doc_begin = R"(
  34. <rml>
  35. <head>
  36. <title>Demo</title>
  37. <style>
  38. body
  39. {
  40. width: 400px;
  41. height: 300px;
  42. margin: auto;
  43. }
  44. )";
  45. static const String doc_end = R"(
  46. </style>
  47. </head>
  48. <body>
  49. <div id="X" class="hello"/>
  50. <span id="Y" class="world"/>
  51. <div id="Z" class="hello world"/>
  52. <div id="P" class="parent">
  53. <h1 id="A"/>
  54. <p id="B"/>
  55. <p id="C"/>
  56. <p id="D"> <span id="D0"/><span id="D1"/> </p>
  57. <h3 id="E"/>
  58. <p id="F"> <span id="F0"/> </p>
  59. <p id="G"/>
  60. <p id="H" class="hello"/>
  61. </div>
  62. <input id="I" type="checkbox" checked/>
  63. </body>
  64. </rml>
  65. )";
  66. struct Selector {
  67. String selector;
  68. String expected_ids;
  69. };
  70. static const std::vector<Selector> selectors =
  71. {
  72. { "span", "Y D0 D1 F0" },
  73. { ".hello", "X Z H" },
  74. { ".hello.world", "Z" },
  75. { "div.hello", "X Z" },
  76. { "body .hello", "X Z H" },
  77. { "body>.hello", "X Z" },
  78. { "body > .hello", "X Z" },
  79. { ".parent *", "A B C D D0 D1 E F F0 G H" },
  80. { ".parent > *", "A B C D E F G H" },
  81. { ":checked", "I" },
  82. { ".parent :nth-child(odd)", "A C D0 E F0 G" },
  83. { ".parent > :nth-child(even)", "B D F H" },
  84. { ":first-child", "X A D0 F0" },
  85. { ":last-child", "D1 F0 H I" },
  86. { "p:nth-child(2)", "B" },
  87. { "h1:nth-child(2)", "" },
  88. { "p:nth-child(3n+1)", "D G" },
  89. { "p:nth-child(3n + 1)", "D G" },
  90. { "#P > :nth-last-child(2n+1)", "B D F H" },
  91. { "#P p:nth-of-type(odd)", "B D G" },
  92. { "p:nth-last-of-type(3n+1)", "D H" },
  93. { ":first-of-type", "X Y A B D0 E F0 I" },
  94. { ":last-of-type", "Y P A D1 E F0 H I" },
  95. { ":only-child", "F0" },
  96. { ":only-of-type", "Y A E F0 I" },
  97. { "span:empty", "Y D0 D1 F0" },
  98. { ".hello.world, #P span, #I", "Z D0 D1 F0 I" },
  99. { "#P * span", "D0 D1 F0" },
  100. };
  101. static void GetMatchingIds(String& matching_ids, Element* element)
  102. {
  103. String id = element->GetId();
  104. if (!id.empty() && element->GetProperty<int>("drag") == (int)Style::Drag::Drag)
  105. {
  106. matching_ids += id + ' ';
  107. }
  108. for (int i = 0; i < element->GetNumChildren(); i++)
  109. {
  110. GetMatchingIds(matching_ids, element->GetChild(i));
  111. }
  112. }
  113. static String ElementListToIds(const ElementList& elements)
  114. {
  115. String result;
  116. for (Element* element : elements)
  117. {
  118. result += element->GetId() + ' ';
  119. }
  120. if (!result.empty())
  121. result.pop_back();
  122. return result;
  123. }
  124. TEST_CASE("Selectors")
  125. {
  126. const Vector2i window_size(1024, 768);
  127. TestsSystemInterface system_interface;
  128. TestsRenderInterface render_interface;
  129. SetRenderInterface(&render_interface);
  130. SetSystemInterface(&system_interface);
  131. Initialise();
  132. Context* context = Rml::CreateContext("main", window_size);
  133. REQUIRE(context);
  134. Debugger::Initialise(context);
  135. for(const Selector& selector : selectors)
  136. {
  137. // Check RCSS document selectors
  138. const String selector_css = selector.selector + " { drag: drag; } ";
  139. const String document_string = doc_begin + selector_css + doc_end;
  140. ElementDocument* document = context->LoadDocumentFromMemory(document_string);
  141. REQUIRE(document);
  142. document->Show();
  143. context->Update();
  144. SUBCASE("RCSS document selectors")
  145. {
  146. String matching_ids;
  147. GetMatchingIds(matching_ids, document);
  148. if (!matching_ids.empty())
  149. matching_ids.pop_back();
  150. CHECK_MESSAGE(matching_ids == selector.expected_ids, "Selector: " << selector.selector);
  151. }
  152. SUBCASE("QuerySelector(All)")
  153. {
  154. ElementList elements;
  155. document->QuerySelectorAll(elements, selector.selector);
  156. String matching_ids = ElementListToIds(elements);
  157. Element* first_element = document->QuerySelector(selector.selector);
  158. if (first_element)
  159. {
  160. CHECK_MESSAGE(first_element == elements[0], "QuerySelector does not return the first match of QuerySelectorAll.");
  161. }
  162. else
  163. {
  164. CHECK_MESSAGE(elements.empty(), "QuerySelector found nothing, while QuerySelectorAll found " << elements.size() << " element(s).");
  165. }
  166. CHECK_MESSAGE(matching_ids == selector.expected_ids, "QuerySelector: " << selector.selector);
  167. }
  168. context->UnloadDocument(document);
  169. }
  170. Rml::Shutdown();
  171. }