DataBinding.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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/TestsShell.h"
  29. #include <RmlUi/Core/Context.h>
  30. #include <RmlUi/Core/DataModelHandle.h>
  31. #include <RmlUi/Core/Element.h>
  32. #include <RmlUi/Core/ElementDocument.h>
  33. #include <doctest.h>
  34. #include <map>
  35. using namespace Rml;
  36. namespace {
  37. static const String document_rml = R"(
  38. <rml>
  39. <head>
  40. <title>Test</title>
  41. <link type="text/rcss" href="/assets/rml.rcss"/>
  42. <link type="text/template" href="/assets/window.rml"/>
  43. <style>
  44. body.window
  45. {
  46. left: 50px;
  47. right: 50px;
  48. top: 30px;
  49. bottom: 30px;
  50. max-width: -1px;
  51. max-height: -1px;
  52. }
  53. div#content
  54. {
  55. text-align: left;
  56. padding: 50px;
  57. box-sizing: border-box;
  58. }
  59. </style>
  60. </head>
  61. <body template="window">
  62. <div data-model="basics">
  63. <input type="text" data-value="i0"/>
  64. <h1>Globals</h1>
  65. <p>{{ i0 }}</p>
  66. <p>{{ i1 }}</p>
  67. <p>{{ i2 }}</p>
  68. <p>{{ i3 }}</p>
  69. <p>{{ s0 }}</p>
  70. <p>{{ s1 }}</p>
  71. <p>{{ s2.val }}</p>
  72. <p>{{ s3.val }}</p>
  73. <p>{{ s4.val }}</p>
  74. <p>{{ s5.val }}</p>
  75. <h1>Basic</h1>
  76. <p>{{ basic.a }}</p>
  77. <p>{{ basic.b }}</p>
  78. <p>{{ basic.c }}</p>
  79. <p>{{ basic.d }}</p>
  80. <p>{{ basic.e }}</p>
  81. <p>{{ basic.f }}</p>
  82. <h1>Wrapped</h1>
  83. <p>{{ wrapped.a.val }}</p>
  84. <p>{{ wrapped.b.val }}</p>
  85. <p>{{ wrapped.c.val }}</p>
  86. <p>{{ wrapped.d.val }}</p>
  87. <p>{{ wrapped.e.val }}</p>
  88. <h1>Pointed</h1>
  89. <p>{{ pointed.a.val }}</p>
  90. <p>{{ pointed.b.val }}</p>
  91. <p>{{ pointed.c.val }}</p>
  92. <h1>Arrays</h1>
  93. <p><span data-for="arrays.a">{{ it }} </span></p>
  94. <p><span data-for="arrays.b">{{ it }} </span></p>
  95. <p><span data-for="arrays.c">{{ it.val }} </span></p>
  96. <p><span data-for="arrays.d">{{ it.val }} </span></p>
  97. <p><span data-for="arrays.e">{{ it.val }} </span></p>
  98. </div>
  99. </body>
  100. </rml>
  101. )";
  102. static const String inside_string_rml = R"(
  103. <rml>
  104. <head>
  105. <title>Test</title>
  106. <link type="text/rcss" href="/assets/rml.rcss"/>
  107. <link type="text/template" href="/assets/window.rml"/>
  108. <style>
  109. body.window
  110. {
  111. left: 50px;
  112. right: 50px;
  113. top: 30px;
  114. bottom: 30px;
  115. max-width: -1px;
  116. max-height: -1px;
  117. }
  118. </style>
  119. </head>
  120. <body template="window">
  121. <div data-model="basics">
  122. <p>{{ i0 }}</p>
  123. <p>{{ 'i0' }}</p>
  124. <p>{{ 'i{}23' }}</p>
  125. <p>before {{ 'i{{test}}23' }} test</p>
  126. <p>a {{ 'i' }} b {{ 'j' }} c</p>
  127. <p>{{i0}}</p>
  128. </div>
  129. </body>
  130. </rml>
  131. )";
  132. struct StringWrap
  133. {
  134. StringWrap(String val = "wrap_default") : val(val) {}
  135. String val;
  136. };
  137. struct Globals
  138. {
  139. int i0 = 0;
  140. int* i1 = new int(1);
  141. UniquePtr<int> i2 = MakeUnique<int>(2);
  142. SharedPtr<int> i3 = MakeShared<int>(3);
  143. String s0 = "s0";
  144. String* s1 = new String("s1");
  145. StringWrap s2 = StringWrap("s2");
  146. StringWrap* s3 = new StringWrap("s3");
  147. UniquePtr<StringWrap> s4 = MakeUnique<StringWrap>("s4");
  148. SharedPtr<StringWrap> s5 = MakeShared<StringWrap>("s5");
  149. // Invalid
  150. const int x0 = 100; // Invalid: const variable
  151. const int* x1 = new int(101); // Invalid: const pointer
  152. UniquePtr<const int> x2 = MakeUnique<int>(102); // Invalid: const pointer
  153. const StringWrap* x3 = new StringWrap("x2"); // Invalid: const pointer
  154. UniquePtr<const StringWrap> x4 = MakeUnique<StringWrap>("x3"); // Invalid: const pointer
  155. } globals;
  156. struct Basic
  157. {
  158. int a = 1;
  159. int* b = new int(2);
  160. int GetC() {
  161. static int v = 5;
  162. return v;
  163. }
  164. int& GetD() {
  165. static int v = 5;
  166. return v;
  167. }
  168. int* GetE() {
  169. static int v = 6;
  170. return &v;
  171. }
  172. UniquePtr<int> GetF() {
  173. return MakeUnique<int>(7);
  174. }
  175. // Invalid: const member
  176. const int x0 = 2;
  177. // Invalid: const pointer
  178. const int* x1 = new int(3);
  179. // Invalid: const qualified member function
  180. int GetX2() const {
  181. return 4;
  182. }
  183. // Invalid: const reference return
  184. const int& GetX3() {
  185. static int g = 7;
  186. return g;
  187. }
  188. // Invalid: const pointer return
  189. const int* GetX4() {
  190. static int h = 8;
  191. return &h;
  192. }
  193. // Invalid: Illegal signature
  194. int GetX5(int) {
  195. return 9;
  196. }
  197. };
  198. struct Wrapped
  199. {
  200. StringWrap a = { "a" };
  201. StringWrap* b = new StringWrap("b");
  202. UniquePtr<StringWrap> c = MakeUnique<StringWrap>("c");
  203. StringWrap& GetD() {
  204. static StringWrap v = { "e" };
  205. return v;
  206. }
  207. StringWrap* GetE() {
  208. static StringWrap v = { "f" };
  209. return &v;
  210. }
  211. // Invalid: const pointer
  212. const StringWrap* x0 = new StringWrap("x0");
  213. // Invalid (run-time): Returning non-scalar variable by value.
  214. StringWrap GetX1() {
  215. return { "x1" };
  216. }
  217. // Invalid (run-time): Returning non-scalar variable by value.
  218. UniquePtr<StringWrap> GetX2() {
  219. return MakeUnique<StringWrap>("x2");
  220. }
  221. };
  222. using StringWrapPtr = UniquePtr<StringWrap>;
  223. struct Pointed
  224. {
  225. StringWrapPtr a = MakeUnique<StringWrap>("a");
  226. StringWrapPtr& GetB() {
  227. static StringWrapPtr v = MakeUnique<StringWrap>("b");
  228. return v;
  229. }
  230. StringWrapPtr* GetC() {
  231. static StringWrapPtr v = MakeUnique<StringWrap>("c");
  232. return &v;
  233. }
  234. // Invalid: We disallow recursive pointer types (pointer to pointer)
  235. StringWrapPtr* x0 = new StringWrapPtr(new StringWrap("x0"));
  236. // Invalid (run-time error): Only scalar data members can be returned by value
  237. StringWrapPtr GetX1() {
  238. return MakeUnique<StringWrap>("x1");
  239. }
  240. };
  241. struct Arrays
  242. {
  243. Vector<int> a = { 10, 11, 12 };
  244. Vector<int*> b = { new int(20), new int(21), new int(22) };
  245. Vector<StringWrap> c = { StringWrap("c1"), StringWrap("c2"), StringWrap("c3") };
  246. Vector<StringWrap*> d = { new StringWrap("d1"), new StringWrap("d2"), new StringWrap("d3") };
  247. Vector<StringWrapPtr> e;
  248. // Invalid: const pointer
  249. Vector<const int*> x0 = { new int(30), new int(31), new int(32) };
  250. // Invalid: const pointer
  251. Vector<UniquePtr<const StringWrap>> x1;
  252. Arrays() {
  253. e.emplace_back(MakeUnique<StringWrap>("e1"));
  254. e.emplace_back(MakeUnique<StringWrap>("e2"));
  255. e.emplace_back(MakeUnique<StringWrap>("e3"));
  256. x1.emplace_back(MakeUnique<StringWrap>("x1_1"));
  257. x1.emplace_back(MakeUnique<StringWrap>("x1_2"));
  258. x1.emplace_back(MakeUnique<StringWrap>("x1_3"));
  259. }
  260. };
  261. DataModelHandle model_handle;
  262. bool InitializeDataBindings(Context* context)
  263. {
  264. Rml::DataModelConstructor constructor = context->CreateDataModel("basics");
  265. if (!constructor)
  266. return false;
  267. if (auto handle = constructor.RegisterStruct<StringWrap>())
  268. {
  269. handle.RegisterMember("val", &StringWrap::val);
  270. }
  271. {
  272. // Globals
  273. constructor.Bind("i0", &globals.i0);
  274. constructor.Bind("i1", &globals.i1);
  275. constructor.Bind("i2", &globals.i2);
  276. constructor.Bind("i3", &globals.i3);
  277. constructor.Bind("s0", &globals.s0);
  278. constructor.Bind("s1", &globals.s1);
  279. constructor.Bind("s2", &globals.s2);
  280. constructor.Bind("s3", &globals.s3);
  281. constructor.Bind("s4", &globals.s4);
  282. constructor.Bind("s5", &globals.s5);
  283. // Invalid: Each of the following should give a compile-time failure.
  284. //constructor.Bind("x0", &globals.x0);
  285. //constructor.Bind("x1", &globals.x1);
  286. //constructor.Bind("x2", &globals.x2);
  287. //constructor.Bind("x3", &globals.x3);
  288. //constructor.Bind("x4", &globals.x4);
  289. }
  290. if (auto handle = constructor.RegisterStruct<Basic>())
  291. {
  292. handle.RegisterMember("a", &Basic::a);
  293. handle.RegisterMember("b", &Basic::b);
  294. handle.RegisterMember("c", &Basic::GetC);
  295. handle.RegisterMember("d", &Basic::GetD);
  296. handle.RegisterMember("e", &Basic::GetE);
  297. handle.RegisterMember("f", &Basic::GetF);
  298. //handle.RegisterMember("x0", &Basic::x0);
  299. //handle.RegisterMember("x1", &Basic::x1);
  300. //handle.RegisterMember("x2", &Basic::GetX2);
  301. //handle.RegisterMember("x3", &Basic::GetX3);
  302. //handle.RegisterMember("x4", &Basic::GetX4);
  303. //handle.RegisterMember("x5", &Basic::GetX5);
  304. }
  305. constructor.Bind("basic", new Basic);
  306. if (auto handle = constructor.RegisterStruct<Wrapped>())
  307. {
  308. handle.RegisterMember("a", &Wrapped::a);
  309. handle.RegisterMember("b", &Wrapped::b);
  310. handle.RegisterMember("c", &Wrapped::c);
  311. handle.RegisterMember("d", &Wrapped::GetD);
  312. handle.RegisterMember("e", &Wrapped::GetE);
  313. //handle.RegisterMember("x0", &Wrapped::x0);
  314. //handle.RegisterMember("x1", &Wrapped::GetX1);
  315. //handle.RegisterMember("x2", &Wrapped::GetX2);
  316. }
  317. constructor.Bind("wrapped", new Wrapped);
  318. if (auto handle = constructor.RegisterStruct<Pointed>())
  319. {
  320. handle.RegisterMember("a", &Pointed::a);
  321. handle.RegisterMember("b", &Pointed::GetB);
  322. handle.RegisterMember("c", &Pointed::GetC);
  323. //handle.RegisterMember("x0", &Pointed::x0);
  324. //handle.RegisterMember("x1", &Pointed::GetX1);
  325. }
  326. constructor.Bind("pointed", new Pointed);
  327. constructor.RegisterArray<decltype(Arrays::a)>();
  328. constructor.RegisterArray<decltype(Arrays::b)>();
  329. constructor.RegisterArray<decltype(Arrays::c)>();
  330. constructor.RegisterArray<decltype(Arrays::d)>();
  331. constructor.RegisterArray<decltype(Arrays::e)>();
  332. //constructor.RegisterArray<decltype(Arrays::x0)>();
  333. //constructor.RegisterArray<decltype(Arrays::x1)>();
  334. if (auto handle = constructor.RegisterStruct<Arrays>())
  335. {
  336. handle.RegisterMember("a", &Arrays::a);
  337. handle.RegisterMember("b", &Arrays::b);
  338. handle.RegisterMember("c", &Arrays::c);
  339. handle.RegisterMember("d", &Arrays::d);
  340. handle.RegisterMember("e", &Arrays::e);
  341. //handle.RegisterMember("x0", &Arrays::x0);
  342. //handle.RegisterMember("x1", &Arrays::x1);
  343. }
  344. constructor.Bind("arrays", new Arrays);
  345. model_handle = constructor.GetModelHandle();
  346. return true;
  347. }
  348. } // Anonymous namespace
  349. TEST_CASE("databinding")
  350. {
  351. Context* context = TestsShell::GetContext();
  352. REQUIRE(context);
  353. REQUIRE(InitializeDataBindings(context));
  354. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  355. REQUIRE(document);
  356. document->Show();
  357. context->Update();
  358. context->Render();
  359. TestsShell::RenderLoop();
  360. document->Close();
  361. TestsShell::ShutdownShell();
  362. }
  363. TEST_CASE("databinding.inside_string")
  364. {
  365. Context* context = TestsShell::GetContext();
  366. REQUIRE(context);
  367. REQUIRE(InitializeDataBindings(context));
  368. ElementDocument* document = context->LoadDocumentFromMemory(inside_string_rml);
  369. REQUIRE(document);
  370. document->Show();
  371. context->Update();
  372. context->Render();
  373. TestsShell::RenderLoop();
  374. CHECK(document->QuerySelector("p:nth-child(4)")->GetInnerRML() == "before i{{test}}23 test");
  375. CHECK(document->QuerySelector("p:nth-child(5)")->GetInnerRML() == "a i b j c");
  376. document->Close();
  377. TestsShell::ShutdownShell();
  378. }