Template.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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")
  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. }