Template.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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-2023 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/Element.h>
  31. #include <RmlUi/Core/ElementDocument.h>
  32. #include <algorithm>
  33. #include <doctest.h>
  34. using namespace Rml;
  35. TEST_CASE("template.body")
  36. {
  37. static const String document_rml = R"(
  38. <rml>
  39. <head>
  40. <link type="text/template" href="/assets/window.rml"/>
  41. <style>
  42. body.window
  43. {
  44. top: 100px;
  45. left: 200px;
  46. width: 600px;
  47. height: 450px;
  48. }
  49. </style>
  50. </head>
  51. <body id="body" class="overridden" template="window">
  52. <p id="p">A paragraph</p>
  53. </body>
  54. </rml>
  55. )";
  56. static const String p_address = "p#p < div#content < div#window < body#body.window < #root#main";
  57. Context* context = TestsShell::GetContext();
  58. REQUIRE(context);
  59. INFO("Expected warning: Body 'class' attribute overridden by template.");
  60. TestsShell::SetNumExpectedWarnings(1);
  61. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  62. TestsShell::SetNumExpectedWarnings(0);
  63. document->Show();
  64. TestsShell::RenderLoop();
  65. Element* el_p = document->GetElementById("p");
  66. REQUIRE(el_p);
  67. CHECK(el_p->GetAddress() == p_address);
  68. document->Close();
  69. TestsShell::ShutdownShell();
  70. }
  71. TEST_CASE("template.body+inline")
  72. {
  73. static const String document_rml = R"(
  74. <rml>
  75. <head>
  76. <link type="text/template" href="/assets/window.rml"/>
  77. <link type="text/template" href="/../Tests/Data/UnitTests/template_basic.rml"/>
  78. <style>
  79. body.window
  80. {
  81. top: 100px;
  82. left: 200px;
  83. width: 600px;
  84. height: 450px;
  85. }
  86. </style>
  87. </head>
  88. <body id="body" template="window">
  89. <p id="p">A paragraph</p>
  90. <div id="basic_wrapper">
  91. <template src="basic">
  92. Hello!<span id="span">World</span>
  93. </template>
  94. </div>
  95. </body>
  96. </rml>
  97. )";
  98. static const String p_address = "p#p < div#content < div#window < body#body.window < #root#main";
  99. static const String span_address = "span#span < p#text < div#basic_wrapper < div#content < div#window < body#body.window < #root#main";
  100. Context* context = TestsShell::GetContext();
  101. REQUIRE(context);
  102. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  103. document->Show();
  104. TestsShell::RenderLoop();
  105. Element* el_p = document->GetElementById("p");
  106. Element* el_span = document->GetElementById("span");
  107. REQUIRE(el_p);
  108. REQUIRE(el_span);
  109. CHECK(el_p->GetAddress() == p_address);
  110. CHECK(el_span->GetAddress() == span_address);
  111. document->Close();
  112. TestsShell::ShutdownShell();
  113. }
  114. TEST_CASE("template.inline")
  115. {
  116. static const String document_rml = R"(
  117. <rml>
  118. <head>
  119. <link type="text/template" href="/assets/window.rml"/>
  120. <style>
  121. body
  122. {
  123. top: 100px;
  124. left: 200px;
  125. width: 600px;
  126. height: 450px;
  127. }
  128. </style>
  129. </head>
  130. <body id="body" class="inline">
  131. <p>Paragraph outside the window.</p>
  132. <div id="template_parent">
  133. <template src="window">
  134. <p id="p">A paragraph</p>
  135. </template>
  136. </div>
  137. </body>
  138. </rml>
  139. )";
  140. static const String p_address = "p#p < div#content < div#window < div#template_parent < body#body.inline < #root#main";
  141. Context* context = TestsShell::GetContext();
  142. REQUIRE(context);
  143. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  144. document->Show();
  145. TestsShell::RenderLoop();
  146. Element* el_p = document->GetElementById("p");
  147. REQUIRE(el_p);
  148. CHECK(el_p->GetAddress() == p_address);
  149. document->Close();
  150. TestsShell::ShutdownShell();
  151. }
  152. TEST_CASE("template.inline+inline.unique")
  153. {
  154. static const String document_rml = R"(
  155. <rml>
  156. <head>
  157. <link type="text/template" href="/assets/window.rml"/>
  158. <link type="text/template" href="/../Tests/Data/UnitTests/template_basic.rml"/>
  159. <style>
  160. body
  161. {
  162. top: 100px;
  163. left: 200px;
  164. width: 600px;
  165. height: 450px;
  166. }
  167. </style>
  168. </head>
  169. <body id="body" class="inline">
  170. <p>Paragraph outside the window.</p>
  171. <div id="template_parent">
  172. <template src="window">
  173. <p id="p">A paragraph</p>
  174. </template>
  175. <div id="basic_wrapper">
  176. <template src="basic">
  177. Hello!<span id="span">World</span>
  178. </template>
  179. </div>
  180. </div>
  181. </body>
  182. </rml>
  183. )";
  184. static const String p_address = "p#p < div#content < div#window < div#template_parent < body#body.inline < #root#main";
  185. static const String span_address = "span#span < p#text < div#basic_wrapper < div#template_parent < body#body.inline < #root#main";
  186. Context* context = TestsShell::GetContext();
  187. REQUIRE(context);
  188. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  189. document->Show();
  190. TestsShell::RenderLoop();
  191. Element* el_p = document->GetElementById("p");
  192. Element* el_span = document->GetElementById("span");
  193. REQUIRE(el_p);
  194. REQUIRE(el_span);
  195. CHECK(el_p->GetAddress() == p_address);
  196. CHECK(el_span->GetAddress() == span_address);
  197. document->Close();
  198. TestsShell::ShutdownShell();
  199. }
  200. TEST_CASE("template.inline+inline.identical.wrapped")
  201. {
  202. static const String document_rml = R"(
  203. <rml>
  204. <head>
  205. <link type="text/template" href="/assets/window.rml"/>
  206. <link type="text/template" href="/../Tests/Data/UnitTests/template_basic.rml"/>
  207. <style>
  208. body
  209. {
  210. top: 100px;
  211. left: 200px;
  212. width: 600px;
  213. height: 450px;
  214. }
  215. p {
  216. border: 1px aqua;
  217. padding: 5px;
  218. margin: 10px;
  219. }
  220. </style>
  221. </head>
  222. <body id="body" class="inline">
  223. <div id="wrap_t1">
  224. <template src="basic">
  225. Enable<span id="s1">X</span>
  226. </template>
  227. </div>
  228. <div id="wrap_t2">
  229. <template src="basic">
  230. Disable<span id="s2">Y</span>
  231. </template>
  232. </div>
  233. </body>
  234. </rml>
  235. )";
  236. static const String s1_address = "span#s1 < p#text < div#wrap_t1 < body#body.inline < #root#main";
  237. static const String s2_address = "span#s2 < p#text < div#wrap_t2 < body#body.inline < #root#main";
  238. Context* context = TestsShell::GetContext();
  239. REQUIRE(context);
  240. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  241. document->Show();
  242. TestsShell::RenderLoop();
  243. CHECK(document->GetElementById("s1")->GetAddress() == s1_address);
  244. CHECK(document->GetElementById("s2")->GetAddress() == s2_address);
  245. CHECK(StringUtilities::StripWhitespace(document->QuerySelector("#wrap_t1 p#text")->GetInnerRML()) == R"(Enable<span id="s1">X</span>)");
  246. document->Close();
  247. TestsShell::ShutdownShell();
  248. }
  249. TEST_CASE("template.inline+inline.identical.siblings")
  250. {
  251. static const String document_rml = R"(
  252. <rml>
  253. <head>
  254. <link type="text/template" href="/assets/window.rml"/>
  255. <link type="text/template" href="/../Tests/Data/UnitTests/template_basic.rml"/>
  256. <style>
  257. body
  258. {
  259. top: 100px;
  260. left: 200px;
  261. width: 600px;
  262. height: 450px;
  263. }
  264. p {
  265. border: 1px aqua;
  266. padding: 5px;
  267. margin: 10px;
  268. }
  269. </style>
  270. </head>
  271. <body id="body" class="inline">
  272. <template src="basic">
  273. Enable<span id="s1">X</span>
  274. </template>
  275. <template src="basic">
  276. Disable<span id="s2">Y</span>
  277. </template>
  278. </body>
  279. </rml>
  280. )";
  281. Context* context = TestsShell::GetContext();
  282. REQUIRE(context);
  283. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  284. document->Show();
  285. TestsShell::RenderLoop();
  286. ElementList p_elements;
  287. document->GetElementsByTagName(p_elements, "p");
  288. REQUIRE(p_elements.size() == 2);
  289. CHECK(StringUtilities::StripWhitespace(p_elements[0]->GetInnerRML()) == R"(Enable<span id="s1">X</span>)");
  290. CHECK(StringUtilities::StripWhitespace(p_elements[1]->GetInnerRML()) == R"(Disable<span id="s2">Y</span>)");
  291. document->Close();
  292. TestsShell::ShutdownShell();
  293. }