LayoutIsolation.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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 "../../../Source/Core/Layout/LayoutNode.h"
  30. #include "../Common/TestsShell.h"
  31. #include "../Common/TypesToString.h"
  32. #include <RmlUi/Core/ComputedValues.h>
  33. #include <RmlUi/Core/Context.h>
  34. #include <RmlUi/Core/Core.h>
  35. #include <RmlUi/Core/Element.h>
  36. #include <RmlUi/Core/ElementDocument.h>
  37. #include <RmlUi/Core/ElementText.h>
  38. #include <RmlUi/Core/ElementUtilities.h>
  39. #include <RmlUi/Debugger/Debugger.h>
  40. #include <doctest.h>
  41. using namespace Rml;
  42. struct ElementLayoutInfo {
  43. ElementLayoutInfo(int tree_depth, const String& address, const Box& box, Vector2f absolute_offset) :
  44. tree_depth(tree_depth), address(address), box(box), absolute_offset(absolute_offset)
  45. {}
  46. int tree_depth;
  47. String address;
  48. Box box;
  49. Vector2f absolute_offset;
  50. bool operator==(const ElementLayoutInfo& other) const
  51. {
  52. return tree_depth == other.tree_depth && address == other.address && box == other.box && absolute_offset == other.absolute_offset;
  53. }
  54. bool operator!=(const ElementLayoutInfo& other) const { return !(*this == other); }
  55. String ToString() const
  56. {
  57. return String(size_t(4 * tree_depth), ' ') +
  58. CreateString("%s :: box = %g x %g (outer %g x %g) :: absolute_offset = %g x %g", address.c_str(), box.GetSize().x, box.GetSize().y,
  59. box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Margin), box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Margin),
  60. absolute_offset.x, absolute_offset.y);
  61. }
  62. };
  63. static Vector<ElementLayoutInfo> CaptureLayoutTree(Element* root_element)
  64. {
  65. Vector<ElementLayoutInfo> layout_info_list;
  66. ElementUtilities::VisitElementsDepthOrder(root_element, [&](Element* element, int tree_depth) {
  67. layout_info_list.emplace_back(tree_depth, element->GetAddress(false, false), element->GetBox(), element->GetAbsoluteOffset());
  68. });
  69. return layout_info_list;
  70. }
  71. static void LogLayoutTree(const Vector<ElementLayoutInfo>& layout_info_list)
  72. {
  73. String message = "Element layout tree:\n";
  74. for (const auto& layout_info : layout_info_list)
  75. message += layout_info.ToString() + "\n";
  76. Rml::Log::Message(Rml::Log::LT_DEBUG, "%s", message.c_str());
  77. }
  78. static const String document_isolation_rml = R"(
  79. <rml>
  80. <head>
  81. <link type="text/rcss" href="/assets/rml.rcss"/>
  82. <style>
  83. body {
  84. width: 800px;
  85. height: 600px;
  86. background-color: #ddd;
  87. font-family: LatoLatin;
  88. font-size: 14px;
  89. }
  90. scrollbarvertical {
  91. width: 12px;
  92. cursor: arrow;
  93. margin-right: -1px;
  94. padding-right: 1px;
  95. }
  96. scrollbarvertical slidertrack {
  97. background-color: #f0f0f0;
  98. }
  99. scrollbarvertical sliderbar {
  100. background-color: #666;
  101. }
  102. .container {
  103. width: 200px;
  104. margin: 20px;
  105. padding: 10px;
  106. background-color: #bbb;
  107. }
  108. .container > div {
  109. background-color: #aaa;
  110. margin: 5px;
  111. padding: 10px;
  112. }
  113. #flex-container {
  114. display: flex;
  115. flex-direction: column;
  116. }
  117. #overflow-container {
  118. overflow: auto;
  119. height: 150px;
  120. }
  121. #absolute-container {
  122. position: absolute;
  123. top: 300px;
  124. left: 300px;
  125. }
  126. </style>
  127. </head>
  128. <body>
  129. <div class="container" id="flex-container">
  130. <div>Flex item 1</div>
  131. <div id="flex-item">Flex item 2</div>
  132. <div id="flex-item-next">Flex item 3</div>
  133. </div>
  134. <div class="container" id="overflow-container">
  135. <div>Overflow item 1</div>
  136. <div id="overflow-item">Overflow item 2</div>
  137. <div>Overflow item 3</div>
  138. <div>Overflow item 4</div>
  139. <div>Overflow item 5</div>
  140. </div>
  141. <div class="container" id="absolute-container">
  142. <div id="absolute-item">Absolute item 1</div>
  143. <div>Absolute item 2</div>
  144. </div>
  145. <div class="container" id="normal-container">
  146. <div id="normal-item">Normal block box</div>
  147. </div>
  148. </body>
  149. </rml>
  150. )";
  151. TEST_CASE("LayoutIsolation.InsideOutsideFormattingContexts")
  152. {
  153. Context* context = TestsShell::GetContext();
  154. ElementDocument* document = context->LoadDocumentFromMemory(document_isolation_rml);
  155. document->Show();
  156. TestsShell::RenderLoop();
  157. SUBCASE("Flex")
  158. {
  159. Element* element = document->GetElementById("flex-item");
  160. Element* next_sibling = element->GetNextSibling();
  161. Element* normal_container = document->GetElementById("normal-container");
  162. Element* normal_item = document->GetElementById("normal-item");
  163. const Vector2f absolute_offset_element = element->GetAbsoluteOffset();
  164. const Vector2f absolute_offset_next_sibling = next_sibling->GetAbsoluteOffset();
  165. const Vector2f absolute_offset_normal_container = normal_container->GetAbsoluteOffset();
  166. const Vector2f absolute_offset_normal_item = normal_item->GetAbsoluteOffset();
  167. rmlui_dynamic_cast<ElementText*>(element->GetFirstChild())->SetText("Modified text that is long enough to cause line break");
  168. TestsShell::RenderLoop();
  169. CHECK(element->GetAbsoluteOffset() == absolute_offset_element);
  170. CHECK(next_sibling->GetAbsoluteOffset() != absolute_offset_next_sibling);
  171. CHECK(normal_container->GetAbsoluteOffset() != absolute_offset_normal_container);
  172. CHECK(normal_item->GetAbsoluteOffset() != absolute_offset_normal_item);
  173. }
  174. SUBCASE("Overflow")
  175. {
  176. Element* element = document->GetElementById("overflow-item");
  177. Element* next_sibling = element->GetNextSibling();
  178. Element* normal_container = document->GetElementById("normal-container");
  179. Element* normal_item = document->GetElementById("normal-item");
  180. const Vector2f absolute_offset_element = element->GetAbsoluteOffset();
  181. const Vector2f absolute_offset_next_sibling = next_sibling->GetAbsoluteOffset();
  182. const Vector2f absolute_offset_normal_container = normal_container->GetAbsoluteOffset();
  183. const Vector2f absolute_offset_normal_item = normal_item->GetAbsoluteOffset();
  184. rmlui_dynamic_cast<ElementText*>(element->GetFirstChild())->SetText("Modified text that is long enough to cause line break");
  185. TestsShell::RenderLoop();
  186. CHECK(element->GetAbsoluteOffset() == absolute_offset_element);
  187. CHECK(next_sibling->GetAbsoluteOffset() != absolute_offset_next_sibling);
  188. CHECK(normal_container->GetAbsoluteOffset() == absolute_offset_normal_container);
  189. CHECK(normal_item->GetAbsoluteOffset() == absolute_offset_normal_item);
  190. }
  191. SUBCASE("Absolute")
  192. {
  193. Element* element = document->GetElementById("absolute-item");
  194. Element* next_sibling = element->GetNextSibling();
  195. REQUIRE(element);
  196. REQUIRE(next_sibling);
  197. const Vector2f absolute_offset_element = element->GetAbsoluteOffset();
  198. const Vector2f absolute_offset_next_sibling = next_sibling->GetAbsoluteOffset();
  199. rmlui_dynamic_cast<ElementText*>(element->GetFirstChild())->SetText("Modified text that is long enough to cause line break");
  200. TestsShell::RenderLoop();
  201. CHECK(element->GetAbsoluteOffset() == absolute_offset_element);
  202. CHECK(next_sibling->GetAbsoluteOffset() != absolute_offset_next_sibling);
  203. }
  204. SUBCASE("Normal")
  205. {
  206. Element* element = document->GetElementById("normal-item");
  207. REQUIRE(element);
  208. const float initial_width = element->GetBox().GetSize().x;
  209. element->SetProperty("width", "250px");
  210. TestsShell::RenderLoop();
  211. float new_width = element->GetBox().GetSize().x;
  212. CHECK(new_width != initial_width);
  213. CHECK(new_width == doctest::Approx(250.0f));
  214. }
  215. document->Close();
  216. TestsShell::ShutdownShell();
  217. }
  218. #ifdef RMLUI_DEBUG
  219. // Wrap all the following tests under this condition, since the format independent debug tracker is only available in debug mode.
  220. TEST_CASE("LayoutIsolation.FullLayoutFormatIndependentCount")
  221. {
  222. Context* context = TestsShell::GetContext();
  223. ElementDocument* document = nullptr;
  224. {
  225. FormattingContextDebugTracker format_independent_tracker;
  226. document = context->LoadDocumentFromMemory(document_isolation_rml);
  227. document->Show();
  228. TestsShell::RenderLoop();
  229. format_independent_tracker.LogMessage();
  230. const auto count_level_1 = std::count_if(format_independent_tracker.GetEntries().begin(), format_independent_tracker.GetEntries().end(),
  231. [](const auto& entry) { return entry.level == 1 && !entry.from_cache; });
  232. CHECK_MESSAGE(count_level_1 == 3, "Expecting one entry for each of flex, overflow, and absolute");
  233. // There are quite a few flex item format occurrences being performed currently. We might reduce the following
  234. // number while working on the flex formatting engine. If this fails for any other reason, it is likely a bug.
  235. CHECK(format_independent_tracker.CountEntries() == 10);
  236. CHECK(format_independent_tracker.CountFormattedEntries() == 10);
  237. }
  238. {
  239. FormattingContextDebugTracker format_independent_tracker;
  240. Element* element = document->GetElementById("flex-item");
  241. rmlui_dynamic_cast<ElementText*>(element->GetFirstChild())->SetText("Modified text that is long enough to cause line break");
  242. TestsShell::RenderLoop();
  243. const auto count_level_1 = std::count_if(format_independent_tracker.GetEntries().begin(), format_independent_tracker.GetEntries().end(),
  244. [](const auto& entry) { return entry.level == 1 && !entry.from_cache; });
  245. CHECK_MESSAGE(count_level_1 == 1, "Only the flexbox should be formatted, the others should be cached");
  246. }
  247. document->Close();
  248. TestsShell::ShutdownShell();
  249. }
  250. static const String document_isolation_absolute_rml = R"(
  251. <rml>
  252. <head>
  253. <link type="text/rcss" href="/assets/rml.rcss"/>
  254. <style>
  255. body {
  256. width: 800px;
  257. height: 600px;
  258. background-color: #ddd;
  259. font-family: LatoLatin;
  260. font-size: 14px;
  261. }
  262. scrollbarvertical {
  263. width: 12px;
  264. cursor: arrow;
  265. margin-right: -1px;
  266. padding-right: 1px;
  267. }
  268. scrollbarvertical slidertrack {
  269. background-color: #f0f0f0;
  270. }
  271. scrollbarvertical sliderbar {
  272. background-color: #666;
  273. }
  274. .container {
  275. width: 200px;
  276. margin: 20px;
  277. padding: 10px;
  278. background-color: #bbb;
  279. }
  280. .container > div {
  281. background-color: #aaa;
  282. margin: 5px;
  283. padding: 10px;
  284. }
  285. #absolute-container {
  286. position: absolute;
  287. top: 300px;
  288. left: 300px;
  289. width: 30%;
  290. }
  291. </style>
  292. </head>
  293. <body>
  294. <div class="container" id="absolute-container">
  295. <div id="absolute-item">Absolutely positioned box</div>
  296. </div>
  297. <div class="container" id="normal-container">
  298. <div id="normal-item">Normal block box</div>
  299. </div>
  300. </body>
  301. </rml>
  302. )";
  303. TEST_CASE("LayoutIsolation.Absolute")
  304. {
  305. Context* context = TestsShell::GetContext();
  306. ElementDocument* document = context->LoadDocumentFromMemory(document_isolation_absolute_rml);
  307. document->Show();
  308. TestsShell::RenderLoop();
  309. FormattingContextDebugTracker format_independent_tracker;
  310. SUBCASE("Modify absolute content")
  311. {
  312. Element* element = document->GetElementById("absolute-item");
  313. const float initial_height = element->GetOffsetHeight();
  314. rmlui_dynamic_cast<ElementText*>(element->GetFirstChild())->SetText("Modified text that is long enough to cause line break");
  315. TestsShell::RenderLoop();
  316. CHECK(element->GetOffsetHeight() != initial_height);
  317. CHECK(format_independent_tracker.CountFormattedEntries() == 1);
  318. }
  319. SUBCASE("Modify absolute width")
  320. {
  321. Element* container = document->GetElementById("absolute-container");
  322. Element* element = document->GetElementById("absolute-item");
  323. const float container_initial_width = container->GetOffsetWidth();
  324. const float element_initial_width = element->GetOffsetWidth();
  325. container->SetProperty("width", "300px");
  326. DebugLogDirtyLayoutTree(document);
  327. document->UpdatePropertiesForDebug();
  328. DebugLogDirtyLayoutTree(document);
  329. TestsShell::RenderLoop();
  330. CHECK(container->GetOffsetWidth() != container_initial_width);
  331. CHECK(element->GetOffsetWidth() != element_initial_width);
  332. // The following could in principle be reduced to 1, since the size of an absolute element should not affect the
  333. // layout of the formatting context it takes part in.
  334. CHECK(format_independent_tracker.CountEntries() == 2);
  335. CHECK(format_independent_tracker.CountFormattedEntries() == 2);
  336. }
  337. SUBCASE("Modify document width")
  338. {
  339. Element* container = document->GetElementById("absolute-container");
  340. Element* element = document->GetElementById("absolute-item");
  341. const float document_width = document->GetOffsetWidth();
  342. const float container_width = container->GetOffsetWidth();
  343. const float element_width = element->GetOffsetWidth();
  344. document->SetProperty("width", "1000px");
  345. TestsShell::RenderLoop();
  346. CHECK(document->GetOffsetWidth() != document_width);
  347. CHECK(container->GetOffsetWidth() != container_width);
  348. CHECK(element->GetOffsetWidth() != element_width);
  349. }
  350. SUBCASE("Modify normal content")
  351. {
  352. Element* element = document->GetElementById("normal-item");
  353. const float initial_height = element->GetOffsetHeight();
  354. LogLayoutTree(CaptureLayoutTree(document));
  355. rmlui_dynamic_cast<ElementText*>(element->GetFirstChild())->SetText("Modified text that is long enough to cause line break");
  356. TestsShell::RenderLoop();
  357. CHECK(element->GetOffsetHeight() != initial_height);
  358. CHECK(format_independent_tracker.CountFormattedEntries() == 1);
  359. }
  360. SUBCASE("Modify normal content and absolute content")
  361. {
  362. Element* absolute_element = document->GetElementById("absolute-item");
  363. Element* normal_element = document->GetElementById("normal-item");
  364. const float absolute_initial_height = absolute_element->GetOffsetHeight();
  365. const float normal_initial_height = normal_element->GetOffsetHeight();
  366. rmlui_dynamic_cast<ElementText*>(absolute_element->GetFirstChild())->SetText("Modified text that is long enough to cause line break");
  367. rmlui_dynamic_cast<ElementText*>(normal_element->GetFirstChild())->SetText("Modified text that is long enough to cause line break");
  368. TestsShell::RenderLoop();
  369. CHECK(absolute_element->GetOffsetHeight() != absolute_initial_height);
  370. CHECK(normal_element->GetOffsetHeight() != normal_initial_height);
  371. CHECK(format_independent_tracker.CountFormattedEntries() == 2);
  372. }
  373. format_independent_tracker.LogMessage();
  374. document->Close();
  375. TestsShell::ShutdownShell();
  376. }
  377. static const String layout_isolation_hidden_absolute_rml = R"(
  378. <rml>
  379. <head>
  380. <title>Demo</title>
  381. <link type="text/rcss" href="/assets/rml.rcss"/>
  382. <style>
  383. body {
  384. font-family: LatoLatin;
  385. font-size: 14px;
  386. border: 5px #a66;
  387. background: #cca;
  388. color: black;
  389. top: 200px;
  390. right: 300px;
  391. bottom: 200px;
  392. left: 300px;
  393. }
  394. div {
  395. width: 300px;
  396. height: 200px;
  397. left: 100px;
  398. top: 100px;
  399. }
  400. .hide {
  401. display: none;
  402. }
  403. .absolute {
  404. position: absolute;
  405. }
  406. .wide {
  407. width: 400px;
  408. }
  409. </style>
  410. </head>
  411. <body id="body">
  412. This is a sample.
  413. <div id="child">Child element</div>
  414. </body>
  415. </rml>
  416. )";
  417. TEST_CASE("LayoutIsolation.HiddenSkipsFormatting")
  418. {
  419. Context* context = TestsShell::GetContext();
  420. ElementDocument* document = context->LoadDocumentFromMemory(layout_isolation_hidden_absolute_rml);
  421. Element* element = document->GetElementById("child");
  422. element->SetClass("hide", true);
  423. SUBCASE("Static") {}
  424. SUBCASE("Absolute")
  425. {
  426. element->SetClass("absolute", true);
  427. }
  428. FormattingContextDebugTracker format_independent_tracker;
  429. document->Show();
  430. TestsShell::RenderLoop();
  431. CHECK(format_independent_tracker.CountFormattedEntries() == 1);
  432. rmlui_dynamic_cast<ElementText*>(element->GetFirstChild())->SetText("Modified text");
  433. CHECK(format_independent_tracker.CountFormattedEntries() == 1);
  434. // Modifying text in a hidden element should not trigger a new layout.
  435. TestsShell::RenderLoop();
  436. CHECK(format_independent_tracker.CountFormattedEntries() == 1);
  437. format_independent_tracker.LogMessage();
  438. document->Close();
  439. TestsShell::ShutdownShell();
  440. }
  441. TEST_CASE("LayoutIsolation.HiddenToggleAndModify")
  442. {
  443. Context* context = TestsShell::GetContext();
  444. ElementDocument* document = context->LoadDocumentFromMemory(layout_isolation_hidden_absolute_rml);
  445. Element* element = document->GetElementById("child");
  446. element->SetClass("hide", true);
  447. float element_offset_left = 0;
  448. SUBCASE("Static")
  449. {
  450. element_offset_left = 305;
  451. }
  452. SUBCASE("Absolute")
  453. {
  454. element->SetClass("absolute", true);
  455. element_offset_left = 405;
  456. }
  457. document->Show();
  458. TestsShell::RenderLoop();
  459. CHECK(element->IsVisible() == false);
  460. element->SetClass("hide", false);
  461. document->UpdatePropertiesForDebug();
  462. TestsShell::RenderLoop();
  463. CHECK(element->GetAbsoluteLeft() == element_offset_left);
  464. CHECK(element->IsVisible() == true);
  465. CHECK(element->GetComputedValues().width().value == 300);
  466. CHECK(element->GetOffsetWidth() == 300.f);
  467. element->SetClass("hide", true);
  468. document->UpdatePropertiesForDebug();
  469. TestsShell::RenderLoop();
  470. CHECK(element->IsVisible() == false);
  471. CHECK(element->GetComputedValues().width().value == 300);
  472. CHECK(element->GetOffsetWidth() == 300.f);
  473. element->SetClass("wide", true);
  474. document->UpdatePropertiesForDebug();
  475. TestsShell::RenderLoop();
  476. CHECK(element->IsVisible() == false);
  477. CHECK(element->GetComputedValues().width().value == 400);
  478. CHECK(element->GetOffsetWidth() == 300.f);
  479. element->SetClass("hide", false);
  480. document->UpdatePropertiesForDebug();
  481. TestsShell::RenderLoop();
  482. CHECK(element->IsVisible() == true);
  483. CHECK(element->GetComputedValues().width().value == 400);
  484. CHECK(element->GetOffsetWidth() == 400.f);
  485. document->Close();
  486. TestsShell::ShutdownShell();
  487. }
  488. static const String layout_isolation_document_rml = R"(
  489. <rml>
  490. <head>
  491. <title>Demo</title>
  492. <link type="text/rcss" href="/assets/rml.rcss"/>
  493. <style>
  494. body {
  495. font-family: LatoLatin;
  496. font-size: 14px;
  497. border: 5px #a66;
  498. background: #cca;
  499. color: black;
  500. top: 200px;
  501. right: 300px;
  502. bottom: 200px;
  503. left: 300px;
  504. }
  505. </style>
  506. </head>
  507. <body>
  508. This is a sample.
  509. </body>
  510. </rml>
  511. )";
  512. TEST_CASE("LayoutIsolation.Document")
  513. {
  514. Context* context = TestsShell::GetContext();
  515. REQUIRE(context->GetDimensions() == Rml::Vector2i{1500, 800});
  516. FormattingContextDebugTracker format_independent_tracker;
  517. ElementDocument* document = context->LoadDocumentFromMemory(layout_isolation_document_rml);
  518. document->Show();
  519. TestsShell::RenderLoop();
  520. CHECK(document->GetOffsetWidth() == 900.f);
  521. CHECK(document->GetOffsetHeight() == 400.f);
  522. SUBCASE("Modify absolute content")
  523. {
  524. context->SetDimensions(Rml::Vector2i{1600, 900});
  525. document->UpdatePropertiesForDebug();
  526. TestsShell::RenderLoop();
  527. CHECK(document->GetOffsetWidth() == 1000.f);
  528. CHECK(document->GetOffsetHeight() == 500.f);
  529. CHECK(format_independent_tracker.CountFormattedEntries() == 2);
  530. }
  531. format_independent_tracker.LogMessage();
  532. document->Close();
  533. TestsShell::ShutdownShell();
  534. }
  535. static const String rml_flexbox_shrink_to_fit_nested = R"(
  536. <rml>
  537. <head>
  538. <title>Flex - Shrink-to-fit 01</title>
  539. <link type="text/rcss" href="/../Tests/Data/style.rcss"/>
  540. <style>
  541. body { width: 10000px; height: 2000px; }
  542. .shrink-to-fit {
  543. float: left;
  544. clear: both;
  545. margin: 10px 0;
  546. border: 2px #e8e8e8;
  547. }
  548. .outer {
  549. display: inline-flex;
  550. border: 1px red;
  551. padding: 30px;
  552. }
  553. .inner {
  554. border: 1px blue;
  555. padding: 30px;
  556. }
  557. </style>
  558. </head>
  559. <body>
  560. <div class="shrink-to-fit">
  561. Before
  562. <div class="outer">
  563. %s
  564. <div id="innermost" class="inner">Flex</div>
  565. %s
  566. </div>
  567. After
  568. </div>
  569. </body>
  570. </rml>
  571. )";
  572. TEST_CASE("LayoutIsolation.FlexFormat.shrink-to-fit")
  573. {
  574. Context* context = TestsShell::GetContext();
  575. REQUIRE(context);
  576. // The number of flex format count listed in this mapping is not an end-all be-all, but we should be made aware if
  577. // it changes, especially for the worse.
  578. const UnorderedMap<int, int> expected_num_nest_levels_versus_num_flex_formats = {
  579. {1, 3},
  580. {2, 8},
  581. {3, 13},
  582. {4, 18},
  583. {5, 23},
  584. {6, 28},
  585. {7, 33},
  586. {8, 38},
  587. {9, 43},
  588. {10, 48},
  589. };
  590. for (int num_nest_levels = 1; num_nest_levels <= 10; num_nest_levels++)
  591. {
  592. const Rml::String document_rml = Rml::CreateString(rml_flexbox_shrink_to_fit_nested.c_str(),
  593. StringUtilities::RepeatString(R"(<div class="inner"><div class="outer">)", num_nest_levels - 1).c_str(),
  594. StringUtilities::RepeatString(R"(</div></div>)", num_nest_levels - 1).c_str());
  595. FormattingContextDebugTracker format_independent_tracker;
  596. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  597. document->Show();
  598. TestsShell::RenderLoop();
  599. int num_flex_formats = 0;
  600. for (const auto& entry : format_independent_tracker.GetEntries())
  601. {
  602. if (entry.context_type == FormattingContextType::Flex && !entry.from_cache)
  603. num_flex_formats += 1;
  604. }
  605. CHECK(num_flex_formats == expected_num_nest_levels_versus_num_flex_formats.at(num_nest_levels));
  606. document->Close();
  607. }
  608. TestsShell::ShutdownShell();
  609. }
  610. TEST_CASE("LayoutIsolation.FlexFormat.shrink-to-fit.cache")
  611. {
  612. Context* context = TestsShell::GetContext();
  613. REQUIRE(context);
  614. constexpr int num_nest_levels = 2;
  615. const Rml::String document_rml = Rml::CreateString(rml_flexbox_shrink_to_fit_nested.c_str(),
  616. StringUtilities::RepeatString(R"(<div class="inner"><div class="outer">)", num_nest_levels - 1).c_str(),
  617. StringUtilities::RepeatString(R"(</div></div>)", num_nest_levels - 1).c_str());
  618. ElementDocument* document = nullptr;
  619. {
  620. FormattingContextDebugTracker format_independent_tracker;
  621. document = context->LoadDocumentFromMemory(document_rml);
  622. document->Show();
  623. TestsShell::RenderLoop();
  624. MESSAGE("Initial layout: ", format_independent_tracker.CountFormattedEntries());
  625. }
  626. {
  627. FormattingContextDebugTracker format_independent_tracker;
  628. document->GetElementById("innermost")->SetInnerRML("Flex");
  629. TestsShell::RenderLoop();
  630. MESSAGE("With cache: ", format_independent_tracker.CountFormattedEntries());
  631. }
  632. {
  633. FormattingContextDebugTracker format_independent_tracker;
  634. document->GetElementById("innermost")->SetInnerRML("Flex");
  635. document->SetAttribute("rmlui-disable-layout-cache", true);
  636. TestsShell::RenderLoop();
  637. MESSAGE("Without cache: ", format_independent_tracker.CountFormattedEntries());
  638. }
  639. document->Close();
  640. TestsShell::ShutdownShell();
  641. }
  642. #endif // RMLUI_DEBUG