DataBinding.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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: none;
  51. max-height: none;
  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. static const String aliasing_rml = R"(
  133. <rml>
  134. <head>
  135. <title>Test</title>
  136. <link type="text/rcss" href="/assets/rml.rcss"/>
  137. <link type="text/template" href="/assets/data-window.rml"/>
  138. </head>
  139. <body data-model="basics">
  140. <p>{{ i0 }}</p>
  141. <p data-alias-differentname="i0">{{ differentname }}</p>
  142. <div data-alias-title="s0" id="w1">
  143. <template src="data-window"></template>
  144. </div>
  145. <div data-alias-title="s1" id="w2">
  146. <template src="data-window"></template>
  147. </div>
  148. </body>
  149. </rml>
  150. )";
  151. struct StringWrap
  152. {
  153. StringWrap(String val = "wrap_default") : val(val) {}
  154. String val;
  155. };
  156. struct Globals
  157. {
  158. int i0 = 0;
  159. int* i1 = new int(1);
  160. UniquePtr<int> i2 = MakeUnique<int>(2);
  161. SharedPtr<int> i3 = MakeShared<int>(3);
  162. String s0 = "s0";
  163. String* s1 = new String("s1");
  164. StringWrap s2 = StringWrap("s2");
  165. StringWrap* s3 = new StringWrap("s3");
  166. UniquePtr<StringWrap> s4 = MakeUnique<StringWrap>("s4");
  167. SharedPtr<StringWrap> s5 = MakeShared<StringWrap>("s5");
  168. // Invalid
  169. const int x0 = 100; // Invalid: const variable
  170. const int* x1 = new int(101); // Invalid: const pointer
  171. UniquePtr<const int> x2 = MakeUnique<int>(102); // Invalid: const pointer
  172. const StringWrap* x3 = new StringWrap("x2"); // Invalid: const pointer
  173. UniquePtr<const StringWrap> x4 = MakeUnique<StringWrap>("x3"); // Invalid: const pointer
  174. } globals;
  175. struct Basic
  176. {
  177. int a = 1;
  178. int* b = new int(2);
  179. int GetC() {
  180. static int v = 5;
  181. return v;
  182. }
  183. int& GetD() {
  184. static int v = 5;
  185. return v;
  186. }
  187. int* GetE() {
  188. static int v = 6;
  189. return &v;
  190. }
  191. UniquePtr<int> GetF() {
  192. return MakeUnique<int>(7);
  193. }
  194. // Invalid: const member
  195. const int x0 = 2;
  196. // Invalid: const pointer
  197. const int* x1 = new int(3);
  198. // Invalid: const qualified member function
  199. int GetX2() const {
  200. return 4;
  201. }
  202. // Invalid: const reference return
  203. const int& GetX3() {
  204. static int g = 7;
  205. return g;
  206. }
  207. // Invalid: const pointer return
  208. const int* GetX4() {
  209. static int h = 8;
  210. return &h;
  211. }
  212. // Invalid: Illegal signature
  213. int GetX5(int) {
  214. return 9;
  215. }
  216. };
  217. struct Wrapped
  218. {
  219. StringWrap a = { "a" };
  220. StringWrap* b = new StringWrap("b");
  221. UniquePtr<StringWrap> c = MakeUnique<StringWrap>("c");
  222. StringWrap& GetD() {
  223. static StringWrap v = { "e" };
  224. return v;
  225. }
  226. StringWrap* GetE() {
  227. static StringWrap v = { "f" };
  228. return &v;
  229. }
  230. // Invalid: const pointer
  231. const StringWrap* x0 = new StringWrap("x0");
  232. // Invalid (run-time): Returning non-scalar variable by value.
  233. StringWrap GetX1() {
  234. return { "x1" };
  235. }
  236. // Invalid (run-time): Returning non-scalar variable by value.
  237. UniquePtr<StringWrap> GetX2() {
  238. return MakeUnique<StringWrap>("x2");
  239. }
  240. };
  241. using StringWrapPtr = UniquePtr<StringWrap>;
  242. struct Pointed
  243. {
  244. StringWrapPtr a = MakeUnique<StringWrap>("a");
  245. StringWrapPtr& GetB() {
  246. static StringWrapPtr v = MakeUnique<StringWrap>("b");
  247. return v;
  248. }
  249. StringWrapPtr* GetC() {
  250. static StringWrapPtr v = MakeUnique<StringWrap>("c");
  251. return &v;
  252. }
  253. // Invalid: We disallow recursive pointer types (pointer to pointer)
  254. StringWrapPtr* x0 = new StringWrapPtr(new StringWrap("x0"));
  255. // Invalid (run-time error): Only scalar data members can be returned by value
  256. StringWrapPtr GetX1() {
  257. return MakeUnique<StringWrap>("x1");
  258. }
  259. };
  260. struct Arrays
  261. {
  262. Vector<int> a = { 10, 11, 12 };
  263. Vector<int*> b = { new int(20), new int(21), new int(22) };
  264. Vector<StringWrap> c = { StringWrap("c1"), StringWrap("c2"), StringWrap("c3") };
  265. Vector<StringWrap*> d = { new StringWrap("d1"), new StringWrap("d2"), new StringWrap("d3") };
  266. Vector<StringWrapPtr> e;
  267. // Invalid: const pointer
  268. Vector<const int*> x0 = { new int(30), new int(31), new int(32) };
  269. // Invalid: const pointer
  270. Vector<UniquePtr<const StringWrap>> x1;
  271. Arrays() {
  272. e.emplace_back(MakeUnique<StringWrap>("e1"));
  273. e.emplace_back(MakeUnique<StringWrap>("e2"));
  274. e.emplace_back(MakeUnique<StringWrap>("e3"));
  275. x1.emplace_back(MakeUnique<StringWrap>("x1_1"));
  276. x1.emplace_back(MakeUnique<StringWrap>("x1_2"));
  277. x1.emplace_back(MakeUnique<StringWrap>("x1_3"));
  278. }
  279. };
  280. DataModelHandle model_handle;
  281. bool InitializeDataBindings(Context* context)
  282. {
  283. Rml::DataModelConstructor constructor = context->CreateDataModel("basics");
  284. if (!constructor)
  285. return false;
  286. if (auto handle = constructor.RegisterStruct<StringWrap>())
  287. {
  288. handle.RegisterMember("val", &StringWrap::val);
  289. }
  290. {
  291. // Globals
  292. constructor.Bind("i0", &globals.i0);
  293. constructor.Bind("i1", &globals.i1);
  294. constructor.Bind("i2", &globals.i2);
  295. constructor.Bind("i3", &globals.i3);
  296. constructor.Bind("s0", &globals.s0);
  297. constructor.Bind("s1", &globals.s1);
  298. constructor.Bind("s2", &globals.s2);
  299. constructor.Bind("s3", &globals.s3);
  300. constructor.Bind("s4", &globals.s4);
  301. constructor.Bind("s5", &globals.s5);
  302. // Invalid: Each of the following should give a compile-time failure.
  303. //constructor.Bind("x0", &globals.x0);
  304. //constructor.Bind("x1", &globals.x1);
  305. //constructor.Bind("x2", &globals.x2);
  306. //constructor.Bind("x3", &globals.x3);
  307. //constructor.Bind("x4", &globals.x4);
  308. }
  309. if (auto handle = constructor.RegisterStruct<Basic>())
  310. {
  311. handle.RegisterMember("a", &Basic::a);
  312. handle.RegisterMember("b", &Basic::b);
  313. handle.RegisterMember("c", &Basic::GetC);
  314. handle.RegisterMember("d", &Basic::GetD);
  315. handle.RegisterMember("e", &Basic::GetE);
  316. handle.RegisterMember("f", &Basic::GetF);
  317. //handle.RegisterMember("x0", &Basic::x0);
  318. //handle.RegisterMember("x1", &Basic::x1);
  319. //handle.RegisterMember("x2", &Basic::GetX2);
  320. //handle.RegisterMember("x3", &Basic::GetX3);
  321. //handle.RegisterMember("x4", &Basic::GetX4);
  322. //handle.RegisterMember("x5", &Basic::GetX5);
  323. }
  324. constructor.Bind("basic", new Basic);
  325. if (auto handle = constructor.RegisterStruct<Wrapped>())
  326. {
  327. handle.RegisterMember("a", &Wrapped::a);
  328. handle.RegisterMember("b", &Wrapped::b);
  329. handle.RegisterMember("c", &Wrapped::c);
  330. handle.RegisterMember("d", &Wrapped::GetD);
  331. handle.RegisterMember("e", &Wrapped::GetE);
  332. //handle.RegisterMember("x0", &Wrapped::x0);
  333. //handle.RegisterMember("x1", &Wrapped::GetX1);
  334. //handle.RegisterMember("x2", &Wrapped::GetX2);
  335. }
  336. constructor.Bind("wrapped", new Wrapped);
  337. if (auto handle = constructor.RegisterStruct<Pointed>())
  338. {
  339. handle.RegisterMember("a", &Pointed::a);
  340. handle.RegisterMember("b", &Pointed::GetB);
  341. handle.RegisterMember("c", &Pointed::GetC);
  342. //handle.RegisterMember("x0", &Pointed::x0);
  343. //handle.RegisterMember("x1", &Pointed::GetX1);
  344. }
  345. constructor.Bind("pointed", new Pointed);
  346. constructor.RegisterArray<decltype(Arrays::a)>();
  347. constructor.RegisterArray<decltype(Arrays::b)>();
  348. constructor.RegisterArray<decltype(Arrays::c)>();
  349. constructor.RegisterArray<decltype(Arrays::d)>();
  350. constructor.RegisterArray<decltype(Arrays::e)>();
  351. //constructor.RegisterArray<decltype(Arrays::x0)>();
  352. //constructor.RegisterArray<decltype(Arrays::x1)>();
  353. if (auto handle = constructor.RegisterStruct<Arrays>())
  354. {
  355. handle.RegisterMember("a", &Arrays::a);
  356. handle.RegisterMember("b", &Arrays::b);
  357. handle.RegisterMember("c", &Arrays::c);
  358. handle.RegisterMember("d", &Arrays::d);
  359. handle.RegisterMember("e", &Arrays::e);
  360. //handle.RegisterMember("x0", &Arrays::x0);
  361. //handle.RegisterMember("x1", &Arrays::x1);
  362. }
  363. constructor.Bind("arrays", new Arrays);
  364. model_handle = constructor.GetModelHandle();
  365. return true;
  366. }
  367. } // Anonymous namespace
  368. TEST_CASE("databinding")
  369. {
  370. Context* context = TestsShell::GetContext();
  371. REQUIRE(context);
  372. REQUIRE(InitializeDataBindings(context));
  373. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  374. REQUIRE(document);
  375. document->Show();
  376. context->Update();
  377. context->Render();
  378. TestsShell::RenderLoop();
  379. document->Close();
  380. TestsShell::ShutdownShell();
  381. }
  382. TEST_CASE("databinding.inside_string")
  383. {
  384. Context* context = TestsShell::GetContext();
  385. REQUIRE(context);
  386. REQUIRE(InitializeDataBindings(context));
  387. ElementDocument* document = context->LoadDocumentFromMemory(inside_string_rml);
  388. REQUIRE(document);
  389. document->Show();
  390. context->Update();
  391. context->Render();
  392. TestsShell::RenderLoop();
  393. CHECK(document->QuerySelector("p:nth-child(4)")->GetInnerRML() == "before i{{test}}23 test");
  394. CHECK(document->QuerySelector("p:nth-child(5)")->GetInnerRML() == "a i b j c");
  395. document->Close();
  396. TestsShell::ShutdownShell();
  397. }
  398. TEST_CASE("databinding.aliasing")
  399. {
  400. Context* context = TestsShell::GetContext();
  401. REQUIRE(context);
  402. REQUIRE(InitializeDataBindings(context));
  403. ElementDocument* document = context->LoadDocumentFromMemory(aliasing_rml);
  404. REQUIRE(document);
  405. document->Show();
  406. context->Update();
  407. context->Render();
  408. TestsShell::RenderLoop();
  409. CHECK(document->QuerySelector("p:nth-child(1)")->GetInnerRML() == document->QuerySelector("p:nth-child(2)")->GetInnerRML());
  410. REQUIRE(document->QuerySelector("#w1 .title"));
  411. REQUIRE(document->QuerySelector("#w2 .title"));
  412. CHECK(document->QuerySelector("#w1 .title")->GetInnerRML() == "s0");
  413. CHECK(document->QuerySelector("#w2 .title")->GetInnerRML() == "s1");
  414. document->Close();
  415. TestsShell::ShutdownShell();
  416. }