LayoutIsolation.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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-2025 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 "../../../Source/Core/Layout/FormattingContextDebug.h"
  29. #include "../Common/TestsShell.h"
  30. #include "../Common/TypesToString.h"
  31. #include <RmlUi/Core/Context.h>
  32. #include <RmlUi/Core/Core.h>
  33. #include <RmlUi/Core/Element.h>
  34. #include <RmlUi/Core/ElementDocument.h>
  35. #include <RmlUi/Core/ElementText.h>
  36. #include <doctest.h>
  37. using namespace Rml;
  38. static const String document_isolation_rml = R"(
  39. <rml>
  40. <head>
  41. <link type="text/rcss" href="/assets/rml.rcss"/>
  42. <style>
  43. body {
  44. width: 800px;
  45. height: 600px;
  46. background-color: #ddd;
  47. font-family: LatoLatin;
  48. font-size: 14px;
  49. }
  50. scrollbarvertical {
  51. width: 12px;
  52. cursor: arrow;
  53. margin-right: -1px;
  54. padding-right: 1px;
  55. }
  56. scrollbarvertical slidertrack {
  57. background-color: #f0f0f0;
  58. }
  59. scrollbarvertical sliderbar {
  60. background-color: #666;
  61. }
  62. .container {
  63. width: 200px;
  64. margin: 20px;
  65. padding: 10px;
  66. background-color: #bbb;
  67. }
  68. .container > div {
  69. background-color: #aaa;
  70. margin: 5px;
  71. padding: 10px;
  72. }
  73. #flex-container {
  74. display: flex;
  75. flex-direction: column;
  76. }
  77. #overflow-container {
  78. overflow: auto;
  79. height: 150px;
  80. }
  81. #absolute-container {
  82. position: absolute;
  83. top: 300px;
  84. left: 300px;
  85. }
  86. </style>
  87. </head>
  88. <body>
  89. <div class="container" id="flex-container">
  90. <div>Flex item 1</div>
  91. <div id="flex-item">Flex item 2</div>
  92. <div id="flex-item-next">Flex item 3</div>
  93. </div>
  94. <div class="container" id="overflow-container">
  95. <div>Overflow item 1</div>
  96. <div id="overflow-item">Overflow item 2</div>
  97. <div>Overflow item 3</div>
  98. <div>Overflow item 4</div>
  99. <div>Overflow item 5</div>
  100. </div>
  101. <div class="container" id="absolute-container">
  102. <div id="absolute-item">Absolute item 1</div>
  103. <div>Absolute item 2</div>
  104. </div>
  105. <div class="container" id="normal-container">
  106. <div id="normal-item">Normal block box</div>
  107. </div>
  108. </body>
  109. </rml>
  110. )";
  111. TEST_CASE("LayoutIsolation.InsideOutsideFormattingContexts")
  112. {
  113. Context* context = TestsShell::GetContext();
  114. ElementDocument* document = context->LoadDocumentFromMemory(document_isolation_rml);
  115. document->Show();
  116. TestsShell::RenderLoop();
  117. SUBCASE("Flex")
  118. {
  119. Element* element = document->GetElementById("flex-item");
  120. Element* next_sibling = element->GetNextSibling();
  121. Element* next_outside_overflow = document->GetElementById("normal-container");
  122. REQUIRE(element);
  123. REQUIRE(next_sibling);
  124. REQUIRE(next_outside_overflow);
  125. const Vector2f absolute_offset_element = element->GetAbsoluteOffset();
  126. const Vector2f absolute_offset_next_sibling = next_sibling->GetAbsoluteOffset();
  127. const Vector2f absolute_offset_next_outside_overflow = next_outside_overflow->GetAbsoluteOffset();
  128. rmlui_dynamic_cast<ElementText*>(element->GetFirstChild())->SetText("Modified text that is long enough to cause line break");
  129. TestsShell::RenderLoop();
  130. CHECK(element->GetAbsoluteOffset() == absolute_offset_element);
  131. CHECK(next_sibling->GetAbsoluteOffset() != absolute_offset_next_sibling);
  132. CHECK(next_outside_overflow->GetAbsoluteOffset() != absolute_offset_next_outside_overflow);
  133. }
  134. SUBCASE("Overflow")
  135. {
  136. Element* element = document->GetElementById("overflow-item");
  137. Element* next_sibling = element->GetNextSibling();
  138. Element* next_outside_overflow = document->GetElementById("normal-container");
  139. REQUIRE(element);
  140. REQUIRE(next_sibling);
  141. REQUIRE(next_outside_overflow);
  142. const Vector2f absolute_offset_element = element->GetAbsoluteOffset();
  143. const Vector2f absolute_offset_next_sibling = next_sibling->GetAbsoluteOffset();
  144. const Vector2f absolute_offset_next_outside_overflow = next_outside_overflow->GetAbsoluteOffset();
  145. rmlui_dynamic_cast<ElementText*>(element->GetFirstChild())->SetText("Modified text that is long enough to cause line break");
  146. TestsShell::RenderLoop();
  147. CHECK(element->GetAbsoluteOffset() == absolute_offset_element);
  148. CHECK(next_sibling->GetAbsoluteOffset() != absolute_offset_next_sibling);
  149. CHECK(next_outside_overflow->GetAbsoluteOffset() == absolute_offset_next_outside_overflow);
  150. }
  151. SUBCASE("Absolute")
  152. {
  153. Element* element = document->GetElementById("absolute-item");
  154. Element* next_sibling = element->GetNextSibling();
  155. REQUIRE(element);
  156. REQUIRE(next_sibling);
  157. const Vector2f absolute_offset_element = element->GetAbsoluteOffset();
  158. const Vector2f absolute_offset_next_sibling = next_sibling->GetAbsoluteOffset();
  159. rmlui_dynamic_cast<ElementText*>(element->GetFirstChild())->SetText("Modified text that is long enough to cause line break");
  160. TestsShell::RenderLoop();
  161. CHECK(element->GetAbsoluteOffset() == absolute_offset_element);
  162. CHECK(next_sibling->GetAbsoluteOffset() != absolute_offset_next_sibling);
  163. }
  164. SUBCASE("Normal")
  165. {
  166. Element* element = document->GetElementById("normal-item");
  167. REQUIRE(element);
  168. const float initial_width = element->GetBox().GetSize().x;
  169. element->SetProperty("width", "250px");
  170. TestsShell::RenderLoop();
  171. float new_width = element->GetBox().GetSize().x;
  172. CHECK(new_width != initial_width);
  173. CHECK(new_width == doctest::Approx(250.0f));
  174. }
  175. document->Close();
  176. TestsShell::ShutdownShell();
  177. }
  178. #ifdef RMLUI_DEBUG
  179. // Wrap all the following tests under this condition, since the format independent debug tracker is only available in debug mode.
  180. TEST_CASE("LayoutIsolation.FullLayoutFormatIndependentCount")
  181. {
  182. Context* context = TestsShell::GetContext();
  183. FormatIndependentDebugTracker format_independent_tracker;
  184. ElementDocument* document = context->LoadDocumentFromMemory(document_isolation_rml);
  185. document->Show();
  186. TestsShell::RenderLoop();
  187. Log::Message(Log::LT_INFO, "%s", format_independent_tracker.ToString().c_str());
  188. const auto count_level_1 = std::count_if(format_independent_tracker.entries.begin(), format_independent_tracker.entries.end(),
  189. [](const auto& entry) { return entry.level == 1; });
  190. CHECK_MESSAGE(count_level_1 == 3, "Expecting one entry for each of flex, overflow, and absolute");
  191. // There are quite a few flex item format occurrences being performed currently. We might reduce the following
  192. // number while working on the flex formatting engine. If this fails for any other reason, it is likely a bug.
  193. CHECK(format_independent_tracker.entries.size() == 10);
  194. document->Close();
  195. TestsShell::ShutdownShell();
  196. }
  197. static const String document_isolation_absolute_rml = R"(
  198. <rml>
  199. <head>
  200. <link type="text/rcss" href="/assets/rml.rcss"/>
  201. <style>
  202. body {
  203. width: 800px;
  204. height: 600px;
  205. background-color: #ddd;
  206. font-family: LatoLatin;
  207. font-size: 14px;
  208. }
  209. scrollbarvertical {
  210. width: 12px;
  211. cursor: arrow;
  212. margin-right: -1px;
  213. padding-right: 1px;
  214. }
  215. scrollbarvertical slidertrack {
  216. background-color: #f0f0f0;
  217. }
  218. scrollbarvertical sliderbar {
  219. background-color: #666;
  220. }
  221. .container {
  222. width: 200px;
  223. margin: 20px;
  224. padding: 10px;
  225. background-color: #bbb;
  226. }
  227. .container > div {
  228. background-color: #aaa;
  229. margin: 5px;
  230. padding: 10px;
  231. }
  232. #absolute-container {
  233. position: absolute;
  234. top: 300px;
  235. left: 300px;
  236. width: 30%;
  237. }
  238. </style>
  239. </head>
  240. <body>
  241. <div class="container" id="absolute-container">
  242. <div id="absolute-item">Absolutely positioned box</div>
  243. </div>
  244. <div class="container" id="normal-container">
  245. <div id="normal-item">Normal block box</div>
  246. </div>
  247. </body>
  248. </rml>
  249. )";
  250. TEST_CASE("LayoutIsolation.Absolute")
  251. {
  252. Context* context = TestsShell::GetContext();
  253. ElementDocument* document = context->LoadDocumentFromMemory(document_isolation_absolute_rml);
  254. document->Show();
  255. TestsShell::RenderLoop();
  256. FormatIndependentDebugTracker format_independent_tracker;
  257. SUBCASE("Modify absolute content")
  258. {
  259. Element* element = document->GetElementById("absolute-item");
  260. const float initial_height = element->GetOffsetHeight();
  261. rmlui_dynamic_cast<ElementText*>(element->GetFirstChild())->SetText("Modified text that is long enough to cause line break");
  262. TestsShell::RenderLoop();
  263. CHECK(element->GetOffsetHeight() != initial_height);
  264. CHECK(format_independent_tracker.entries.size() == 1);
  265. }
  266. SUBCASE("Modify absolute width")
  267. {
  268. Element* container = document->GetElementById("absolute-container");
  269. Element* element = document->GetElementById("absolute-item");
  270. const float container_initial_width = container->GetOffsetWidth();
  271. const float element_initial_width = element->GetOffsetWidth();
  272. container->SetProperty("width", "300px");
  273. TestsShell::RenderLoop();
  274. CHECK(container->GetOffsetWidth() != container_initial_width);
  275. CHECK(element->GetOffsetWidth() != element_initial_width);
  276. CHECK(format_independent_tracker.entries.size() == 1);
  277. }
  278. SUBCASE("Modify document width")
  279. {
  280. Element* container = document->GetElementById("absolute-container");
  281. Element* element = document->GetElementById("absolute-item");
  282. const float document_width = document->GetOffsetWidth();
  283. const float container_width = container->GetOffsetWidth();
  284. const float element_width = element->GetOffsetWidth();
  285. document->SetProperty("width", "1000px");
  286. TestsShell::RenderLoop();
  287. CHECK(document->GetOffsetWidth() != document_width);
  288. CHECK(container->GetOffsetWidth() != container_width);
  289. CHECK(element->GetOffsetWidth() != element_width);
  290. }
  291. SUBCASE("Modify normal content")
  292. {
  293. Element* element = document->GetElementById("normal-item");
  294. const float initial_height = element->GetOffsetHeight();
  295. rmlui_dynamic_cast<ElementText*>(element->GetFirstChild())->SetText("Modified text that is long enough to cause line break");
  296. TestsShell::RenderLoop();
  297. CHECK(element->GetOffsetHeight() != initial_height);
  298. CHECK(format_independent_tracker.entries.size() == 1);
  299. }
  300. SUBCASE("Modify normal content and absolute content")
  301. {
  302. Element* absolute_element = document->GetElementById("absolute-item");
  303. Element* normal_element = document->GetElementById("normal-item");
  304. const float absolute_initial_height = absolute_element->GetOffsetHeight();
  305. const float normal_initial_height = normal_element->GetOffsetHeight();
  306. rmlui_dynamic_cast<ElementText*>(absolute_element->GetFirstChild())->SetText("Modified text that is long enough to cause line break");
  307. rmlui_dynamic_cast<ElementText*>(normal_element->GetFirstChild())->SetText("Modified text that is long enough to cause line break");
  308. TestsShell::RenderLoop();
  309. CHECK(absolute_element->GetOffsetHeight() != absolute_initial_height);
  310. CHECK(normal_element->GetOffsetHeight() != normal_initial_height);
  311. CHECK(format_independent_tracker.entries.size() == 2);
  312. }
  313. Log::Message(Log::LT_INFO, "%s", format_independent_tracker.ToString().c_str());
  314. document->Close();
  315. TestsShell::ShutdownShell();
  316. }
  317. #endif // RMLUI_DEBUG