Template.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. static const String document_body_template_rml = R"(
  36. <rml>
  37. <head>
  38. <link type="text/template" href="/assets/window.rml"/>
  39. <style>
  40. body.window
  41. {
  42. top: 100px;
  43. left: 200px;
  44. width: 600px;
  45. height: 450px;
  46. }
  47. </style>
  48. </head>
  49. <body id="body" class="overridden" template="window">
  50. <p id="p">A paragraph</p>
  51. </body>
  52. </rml>
  53. )";
  54. static const String p_address_body_template = "p#p < div#content < div#window < body#body.window < #root#main";
  55. static const String document_body_and_inline_template_rml = R"(
  56. <rml>
  57. <head>
  58. <link type="text/template" href="/assets/window.rml"/>
  59. <link type="text/template" href="/../Tests/Data/UnitTests/template_basic.rml"/>
  60. <style>
  61. body.window
  62. {
  63. top: 100px;
  64. left: 200px;
  65. width: 600px;
  66. height: 450px;
  67. }
  68. </style>
  69. </head>
  70. <body id="body" template="window">
  71. <p id="p">A paragraph</p>
  72. <div id="basic_wrapper">
  73. <template src="basic">
  74. Hello!<span id="span">World</span>
  75. </template>
  76. </div>
  77. </body>
  78. </rml>
  79. )";
  80. static const String span_address_body_template = "span#span < p#text < div#basic_wrapper < div#content < div#window < body#body.window < #root#main";
  81. static const String document_inline_template_rml = R"(
  82. <rml>
  83. <head>
  84. <link type="text/template" href="/assets/window.rml"/>
  85. <style>
  86. body
  87. {
  88. top: 100px;
  89. left: 200px;
  90. width: 600px;
  91. height: 450px;
  92. }
  93. </style>
  94. </head>
  95. <body id="body" class="inline">
  96. <p>Paragraph outside the window.</p>
  97. <div id="template_parent">
  98. <template src="window">
  99. <p id="p">A paragraph</p>
  100. </template>
  101. </div>
  102. </body>
  103. </rml>
  104. )";
  105. static const String p_address_inline_template = "p#p < div#content < div#window < div#template_parent < body#body.inline < #root#main";
  106. static const String document_double_inline_template_rml = R"(
  107. <rml>
  108. <head>
  109. <link type="text/template" href="/assets/window.rml"/>
  110. <link type="text/template" href="/../Tests/Data/UnitTests/template_basic.rml"/>
  111. <style>
  112. body
  113. {
  114. top: 100px;
  115. left: 200px;
  116. width: 600px;
  117. height: 450px;
  118. }
  119. </style>
  120. </head>
  121. <body id="body" class="inline">
  122. <p>Paragraph outside the window.</p>
  123. <div id="template_parent">
  124. <template src="window">
  125. <p id="p">A paragraph</p>
  126. </template>
  127. <div id="basic_wrapper">
  128. <template src="basic">
  129. Hello!<span id="span">World</span>
  130. </template>
  131. </div>
  132. </div>
  133. </body>
  134. </rml>
  135. )";
  136. static const String span_address_inline_template = "span#span < p#text < div#basic_wrapper < div#template_parent < body#body.inline < #root#main";
  137. TEST_CASE("template")
  138. {
  139. Context* context = TestsShell::GetContext();
  140. REQUIRE(context);
  141. SUBCASE("body")
  142. {
  143. INFO("Expected warning: Body 'class' attribute overridden by template.");
  144. TestsShell::SetNumExpectedWarnings(1);
  145. ElementDocument* document = context->LoadDocumentFromMemory(document_body_template_rml);
  146. TestsShell::SetNumExpectedWarnings(0);
  147. document->Show();
  148. TestsShell::RenderLoop();
  149. Element* el_p = document->GetElementById("p");
  150. REQUIRE(el_p);
  151. CHECK(el_p->GetAddress() == p_address_body_template);
  152. document->Close();
  153. }
  154. SUBCASE("body+inline")
  155. {
  156. ElementDocument* document = context->LoadDocumentFromMemory(document_body_and_inline_template_rml);
  157. document->Show();
  158. TestsShell::RenderLoop();
  159. Element* el_p = document->GetElementById("p");
  160. Element* el_span = document->GetElementById("span");
  161. REQUIRE(el_p);
  162. REQUIRE(el_span);
  163. CHECK(el_p->GetAddress() == p_address_body_template);
  164. CHECK(el_span->GetAddress() == span_address_body_template);
  165. document->Close();
  166. }
  167. SUBCASE("inline")
  168. {
  169. ElementDocument* document = context->LoadDocumentFromMemory(document_inline_template_rml);
  170. document->Show();
  171. TestsShell::RenderLoop();
  172. Element* el_p = document->GetElementById("p");
  173. REQUIRE(el_p);
  174. CHECK(el_p->GetAddress() == p_address_inline_template);
  175. document->Close();
  176. }
  177. SUBCASE("inline+inline")
  178. {
  179. ElementDocument* document = context->LoadDocumentFromMemory(document_double_inline_template_rml);
  180. document->Show();
  181. TestsShell::RenderLoop();
  182. Element* el_p = document->GetElementById("p");
  183. Element* el_span = document->GetElementById("span");
  184. REQUIRE(el_p);
  185. REQUIRE(el_span);
  186. CHECK(el_p->GetAddress() == p_address_inline_template);
  187. CHECK(el_span->GetAddress() == span_address_inline_template);
  188. document->Close();
  189. }
  190. TestsShell::ShutdownShell();
  191. }