Skip to content

Commit

Permalink
Make flex containers and tables with fixed width work in shrink-to-fi…
Browse files Browse the repository at this point in the history
…t contexts, see #520
  • Loading branch information
mikke89 committed Oct 11, 2023
1 parent ee2986e commit 8a94ab7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Source/Core/Layout/ContainerBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@ bool FlexContainer::Close(const Vector2f content_overflow_size, const Box& box,
return true;
}

float FlexContainer::GetShrinkToFitWidth() const
{
// We don't currently support shrink-to-fit layout of flex containers. However, for the trivial case of a fixed
// width, we simply return that.
if (element->GetComputedValues().width().type == Style::Width::Type::Length)
return box.GetSize().x;

return 0.0f;
}

String FlexContainer::DebugDumpTree(int depth) const
{
return String(depth * 2, ' ') + "FlexContainer" + " | " + LayoutDetails::GetDebugElementName(element);
Expand All @@ -291,6 +301,16 @@ void TableWrapper::Close(const Vector2f content_overflow_size, const Box& box, f
SetElementBaseline(element_baseline);
}

float TableWrapper::GetShrinkToFitWidth() const
{
// We don't currently support shrink-to-fit layout of tables. However, for the trivial case of a fixed width, we
// simply return that.
if (element->GetComputedValues().width().type == Style::Width::Type::Length)
return box.GetSize().x;

return 0.0f;
}

String TableWrapper::DebugDumpTree(int depth) const
{
return String(depth * 2, ' ') + "TableWrapper" + " | " + LayoutDetails::GetDebugElementName(element);
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/Layout/ContainerBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ class FlexContainer final : public ContainerBox {
// @returns True if it succeeds, otherwise false if it needs to be formatted again because scrollbars were enabled.
bool Close(const Vector2f content_overflow_size, const Box& box, float element_baseline);

float GetShrinkToFitWidth() const override;

const Box* GetIfBox() const override { return &box; }
String DebugDumpTree(int depth) const override;

Expand All @@ -152,6 +154,8 @@ class TableWrapper final : public ContainerBox {
// Submits the formatted box to the table element, and propagates any uncaught overflow to this box.
void Close(const Vector2f content_overflow_size, const Box& box, float element_baseline);

float GetShrinkToFitWidth() const override;

const Box* GetIfBox() const override { return &box; }
String DebugDumpTree(int depth) const override;

Expand Down
67 changes: 67 additions & 0 deletions Tests/Data/VisualTests/shrink_to_fit_04.rml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<rml>
<head>
<title>Shrink-to-fit 4</title>
<link type="text/rcss" href="../style.rcss"/>
<link rel="help" href="https://www.w3.org/TR/CSS21/visudet.html#shrink-to-fit-float" />
<meta name="Description" content="Shrink-to-fit should respect definite widths of inner elements. This also applies to flex containers and tables, even though we don't currently support shrink-to-fit layout for them." />
<style>
body {
background: #ddd;
color: #444;
}
body > div {
float: left;
clear: left;
margin-top: 20dp;
color: black;
}
.wrapper {
border: 2dp #888;
background: #f00;
}
.fixed {
width: 100dp;
padding: 10dp 5dp 10dp 15dp;
box-sizing: border-box;
background: #ccf;
}
.flex {
display: flex;
}
</style>
</head>

<body>
<p>The following boxes should all appear the same, with a border wrapped all the way around the background and no red visible.</p>

<div>
<div class="wrapper">
<div class="fixed">Hello</div>
</div>
</div>
<div>
<div class="wrapper">
<div>
<div class="fixed">Hello</div>
</div>
</div>
</div>
<div>
<div class="wrapper">
<div class="flex fixed"><div>Hello</div></div>
</div>
</div>
<div>
<div class="wrapper">
<div>
<div class="flex fixed"><div>Hello</div></div>
</div>
</div>
</div>
<div>
<div class="wrapper">
<table class="fixed"><td>Hello</td></table>
</div>
</div>
</body>
</rml>

0 comments on commit 8a94ab7

Please sign in to comment.