Skip to content

Commit 6922a84

Browse files
committed
flex: fixed rendering issues
1 parent ce7d5a1 commit 6922a84

File tree

2 files changed

+50
-34
lines changed

2 files changed

+50
-34
lines changed

include/litehtml/render_flex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ namespace litehtml
7474
};
7575

7676
std::list<flex_line> get_lines(const containing_block_context &self_size, formatting_context *fmt_ctx, bool is_row_direction,
77-
int container_main_size);
77+
int container_main_size, bool single_line);
7878
int _render_content(int x, int y, bool second_pass, const containing_block_context &self_size, formatting_context* fmt_ctx) override;
7979

8080
public:

src/render_flex.cpp

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -193,20 +193,10 @@ int litehtml::render_item_flex::_render_content(int x, int y, bool second_pass,
193193
case flex_direction_column:
194194
is_row_direction = false;
195195
reverse = false;
196-
container_main_size = self_size.height;
197-
if (css().get_box_sizing() == box_sizing_border_box)
198-
{
199-
container_main_size -= box_sizing_height();
200-
}
201196
break;
202197
case flex_direction_column_reverse:
203198
is_row_direction = false;
204199
reverse = true;
205-
container_main_size = self_size.height;
206-
if (css().get_box_sizing() == box_sizing_border_box)
207-
{
208-
container_main_size -= box_sizing_height();
209-
}
210200
break;
211201
case flex_direction_row:
212202
is_row_direction = true;
@@ -218,8 +208,29 @@ int litehtml::render_item_flex::_render_content(int x, int y, bool second_pass,
218208
break;
219209
}
220210

211+
bool single_line = css().get_flex_wrap() == flex_wrap_nowrap;
212+
bool fit_container = false;
213+
214+
if(!is_row_direction)
215+
{
216+
if(self_size.height.type != containing_block_context::cbc_value_type_auto)
217+
{
218+
container_main_size = self_size.height;
219+
if (css().get_box_sizing() == box_sizing_border_box)
220+
{
221+
container_main_size -= box_sizing_height();
222+
}
223+
} else
224+
{
225+
// Direction columns, height is auto - always in single line
226+
container_main_size = 0;
227+
single_line = true;
228+
fit_container = true;
229+
}
230+
}
231+
221232
// Split flex items to lines
222-
std::list<flex_line> lines = get_lines(self_size, fmt_ctx, is_row_direction, container_main_size);
233+
std::list<flex_line> lines = get_lines(self_size, fmt_ctx, is_row_direction, container_main_size, single_line);
223234

224235
// Resolving Flexible Lengths
225236
// REF: https://www.w3.org/TR/css-flexbox-1/#resolve-flexible-lengths
@@ -238,7 +249,10 @@ int litehtml::render_item_flex::_render_content(int x, int y, bool second_pass,
238249
ret_width += ln.base_size;
239250
}
240251

241-
ln.distribute_free_space(container_main_size);
252+
if(!fit_container)
253+
{
254+
ln.distribute_free_space(container_main_size);
255+
}
242256

243257
if(is_row_direction)
244258
{
@@ -267,8 +281,6 @@ int litehtml::render_item_flex::_render_content(int x, int y, bool second_pass,
267281
item.el->render(el_x,
268282
el_y,
269283
self_size.new_width(el_ret_width), fmt_ctx, false);
270-
// TODO: must be rendered into the specified height
271-
item.el->pos().height = item.main_size - item.el->content_offset_height();
272284
ln.cross_size = std::max(ln.cross_size, item.el->width());
273285
el_y += item.el->height();
274286
}
@@ -278,24 +290,21 @@ int litehtml::render_item_flex::_render_content(int x, int y, bool second_pass,
278290
}
279291

280292
int free_cross_size = 0;
281-
if(sum_cross_size)
293+
if (is_row_direction)
282294
{
283-
if (is_row_direction)
295+
if (self_size.height.type != containing_block_context::cbc_value_type_auto)
284296
{
285-
if (self_size.height.type != containing_block_context::cbc_value_type_auto)
297+
int height = self_size.height;
298+
if (src_el()->css().get_box_sizing() == box_sizing_border_box)
286299
{
287-
int height = self_size.height;
288-
if (src_el()->css().get_box_sizing() == box_sizing_border_box)
289-
{
290-
height -= box_sizing_height();
291-
}
292-
free_cross_size = height - sum_cross_size;
300+
height -= box_sizing_height();
293301
}
294-
} else
295-
{
296-
free_cross_size = self_size.render_width - sum_cross_size;
297-
ret_width = sum_cross_size;
302+
free_cross_size = height - sum_cross_size;
298303
}
304+
} else
305+
{
306+
free_cross_size = self_size.render_width - sum_cross_size;
307+
ret_width = sum_cross_size;
299308
}
300309

301310
// Find line cross size and align items
@@ -377,8 +386,11 @@ int litehtml::render_item_flex::_render_content(int x, int y, bool second_pass,
377386
break;
378387
default:
379388
item.el->pos().y = el_y + item.el->content_offset_top();
380-
// TODO: must be rendered into the specified height
381-
item.el->pos().height = ln.cross_size - item.el->content_offset_height();
389+
if(item.el->css().get_height().is_predefined())
390+
{
391+
// TODO: must be rendered into the specified height
392+
item.el->pos().height = ln.cross_size - item.el->content_offset_height();
393+
}
382394
break;
383395
}
384396
m_pos.height = std::max(m_pos.height, item.el->bottom());
@@ -458,8 +470,11 @@ int litehtml::render_item_flex::_render_content(int x, int y, bool second_pass,
458470
item.el->render(el_x,
459471
item.el->pos().y - item.el->content_offset_top(),
460472
self_size.new_width(ln.cross_size), fmt_ctx, false);
461-
// TODO: must be rendered into the specified height
462-
item.el->pos().height = item.main_size - item.el->content_offset_height();
473+
if(item.el->css().get_width().is_predefined())
474+
{
475+
// TODO: must be rendered into the specified height
476+
item.el->pos().height = item.main_size - item.el->content_offset_height();
477+
}
463478
break;
464479
}
465480
m_pos.height = std::max(m_pos.height, item.el->bottom());
@@ -610,7 +625,8 @@ litehtml::render_item_flex::flex_line::distribute_free_space(int container_main_
610625

611626
std::list<litehtml::render_item_flex::flex_line> litehtml::render_item_flex::get_lines(const litehtml::containing_block_context &self_size,
612627
litehtml::formatting_context *fmt_ctx,
613-
bool is_row_direction, int container_main_size)
628+
bool is_row_direction, int container_main_size,
629+
bool single_line)
614630
{
615631
std::list<flex_line> lines;
616632
flex_line line;
@@ -748,7 +764,7 @@ std::list<litehtml::render_item_flex::flex_line> litehtml::render_item_flex::get
748764
// Add flex items to lines
749765
for(auto& item : items)
750766
{
751-
if(!line.items.empty() && css().get_flex_wrap() != flex_wrap_nowrap && line.base_size + item.base_size > container_main_size)
767+
if(!line.items.empty() && !single_line && line.base_size + item.base_size > container_main_size)
752768
{
753769
lines.push_back(line);
754770
line.clear();

0 commit comments

Comments
 (0)