Table.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 <RmlUi/Core/Types.h>
  33. #include <doctest.h>
  34. #include <nanobench.h>
  35. using namespace ankerl;
  36. using namespace Rml;
  37. static const String rml_table_document = R"(
  38. <rml>
  39. <head>
  40. <title>Table</title>
  41. <link type="text/rcss" href="/../Tests/Data/style.rcss"/>
  42. <style>
  43. table {
  44. border-width: 20px 5px 0;
  45. color: #333;
  46. text-align: center;
  47. }
  48. table, table * {
  49. border-color: #666;
  50. }
  51. td {
  52. padding: 15px 5px;
  53. height: 47px;
  54. }
  55. col {
  56. background: #3d3;
  57. }
  58. col:first-child {
  59. width: 150px;
  60. background: #6df;
  61. border-right-width: 3px;
  62. }
  63. col:last-of-type {
  64. background: #dd3;
  65. }
  66. thead {
  67. color: black;
  68. background: #fff5;
  69. border-bottom: 3px #666;
  70. }
  71. tbody tr {
  72. border-bottom: 1px #666a;
  73. }
  74. tbody tr:last-child {
  75. border-bottom: 0;
  76. }
  77. tbody tr:hover {
  78. background: #fff5;
  79. }
  80. tfoot {
  81. background: #666;
  82. color: #ccc;
  83. }
  84. tfoot td {
  85. padding-top: 0px;
  86. padding-bottom: 0px;
  87. text-align: right;
  88. height: 20px;
  89. }
  90. </style>
  91. </head>
  92. <body>
  93. </body>
  94. </rml>
  95. )";
  96. static const String rml_table_element = R"(
  97. <table>
  98. <col/>
  99. <col span="2"/>
  100. <col/>
  101. <thead>
  102. <tr>
  103. <td>A</td>
  104. <td colspan="2">B</td>
  105. <td>C</td>
  106. </tr>
  107. </thead>
  108. <tbody>
  109. <tr>
  110. <td>D</td>
  111. <td>E</td>
  112. <td>F</td>
  113. <td>G</td>
  114. </tr>
  115. <tr>
  116. <td>H</td>
  117. <td>I</td>
  118. <td>J</td>
  119. <td>K</td>
  120. </tr>
  121. </tbody>
  122. <tfoot>
  123. <tr>
  124. <td colspan="4">[1] Footnote</td>
  125. </tr>
  126. </tfoot>
  127. </table>
  128. )";
  129. static const String rml_inlineblock_document = R"(
  130. <rml>
  131. <head>
  132. <title>Table inline-block</title>
  133. <link type="text/rcss" href="/../Tests/Data/style.rcss"/>
  134. <style>
  135. table {
  136. display: block;
  137. border-width: 20px 5px 0;
  138. color: #333;
  139. text-align: center;
  140. }
  141. body * {
  142. border-color: #666;
  143. }
  144. td {
  145. display: inline-block;
  146. box-sizing: border-box;
  147. padding: 15px 5px;
  148. width: 25%;
  149. height: 47px;
  150. background: #3d3;
  151. }
  152. td.span2 { width: 50%; }
  153. td.span4 { width: 100%; }
  154. tr {
  155. display: block;
  156. }
  157. td:first-child {
  158. background: #6df;
  159. border-right-width: 3px;
  160. }
  161. td:last-of-type {
  162. background: #dd3;
  163. }
  164. thead {
  165. display: block;
  166. color: black;
  167. background: #fff5;
  168. border-bottom: 3px #666;
  169. }
  170. tbody {
  171. display: block;
  172. }
  173. tbody tr {
  174. border-bottom: 1px #666a;
  175. }
  176. tbody tr:last-child {
  177. border-bottom: 0;
  178. }
  179. tbody tr:hover {
  180. background: #fff5;
  181. }
  182. tfoot {
  183. display: block;
  184. background: #666;
  185. color: #ccc;
  186. }
  187. tfoot td {
  188. padding-top: 0px;
  189. padding-bottom: 0px;
  190. text-align: right;
  191. height: 20px;
  192. }
  193. </style>
  194. </head>
  195. <body>
  196. </body>
  197. </rml>
  198. )";
  199. static const String rml_inline_block_element = R"(
  200. <table>
  201. <thead>
  202. <tr>
  203. <td>A</td>
  204. <td class="span2">B</td>
  205. <td>C</td>
  206. </tr>
  207. </thead>
  208. <tbody>
  209. <tr>
  210. <td>D</td>
  211. <td>E</td>
  212. <td>F</td>
  213. <td>G</td>
  214. </tr>
  215. <tr>
  216. <td>H</td>
  217. <td>I</td>
  218. <td>J</td>
  219. <td>K</td>
  220. </tr>
  221. </tbody>
  222. <tfoot>
  223. <tr>
  224. <td style="background: transparent" class="span4">[1] Footnote</td>
  225. </tr>
  226. </tfoot>
  227. </table>
  228. )";
  229. TEST_CASE("table_basic")
  230. {
  231. Context* context = TestsShell::GetContext();
  232. REQUIRE(context);
  233. ElementDocument* document = context->LoadDocumentFromMemory(rml_table_document);
  234. REQUIRE(document);
  235. document->Show();
  236. nanobench::Bench bench;
  237. bench.title("Table basic");
  238. bench.relative(true);
  239. document->SetInnerRML(rml_table_element);
  240. context->Update();
  241. context->Render();
  242. TestsShell::RenderLoop();
  243. const String msg = TestsShell::GetRenderStats();
  244. MESSAGE(msg);
  245. bench.run("Update (unmodified)", [&] { context->Update(); });
  246. bench.run("Render", [&] { context->Render(); });
  247. bench.run("SetInnerRML", [&] { document->SetInnerRML(rml_table_element); });
  248. bench.run("SetInnerRML + Update", [&] {
  249. document->SetInnerRML(rml_table_element);
  250. context->Update();
  251. });
  252. bench.run("SetInnerRML + Update + Render", [&] {
  253. document->SetInnerRML(rml_table_element);
  254. context->Update();
  255. context->Render();
  256. });
  257. document->Close();
  258. }
  259. TEST_CASE("table_inline-block")
  260. {
  261. Context* context = TestsShell::GetContext();
  262. REQUIRE(context);
  263. ElementDocument* document = context->LoadDocumentFromMemory(rml_inlineblock_document);
  264. REQUIRE(document);
  265. document->Show();
  266. nanobench::Bench bench;
  267. bench.title("Table inline-block");
  268. bench.relative(true);
  269. document->SetInnerRML(rml_inline_block_element);
  270. context->Update();
  271. context->Render();
  272. TestsShell::RenderLoop();
  273. const String msg = TestsShell::GetRenderStats();
  274. MESSAGE(msg);
  275. bench.run("Update (unmodified)", [&] { context->Update(); });
  276. bench.run("Render", [&] { context->Render(); });
  277. bench.run("SetInnerRML", [&] { document->SetInnerRML(rml_inline_block_element); });
  278. bench.run("SetInnerRML + Update", [&] {
  279. document->SetInnerRML(rml_inline_block_element);
  280. context->Update();
  281. });
  282. bench.run("SetInnerRML + Update + Render", [&] {
  283. document->SetInnerRML(rml_inline_block_element);
  284. context->Update();
  285. context->Render();
  286. });
  287. document->Close();
  288. }