LayoutIsolation.cpp 24 KB

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