Skip to content

Commit 691965b

Browse files
committed
flex: fix auto-shrink tests
1 parent 96c79a0 commit 691965b

21 files changed

+88
-40
lines changed

include/litehtml/types.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,10 @@ namespace litehtml
197197

198198
enum cbc_size_mode
199199
{
200-
cbc_size_mode_normal = 0x00,
201-
cbc_size_mode_exact_width = 0x01,
202-
cbc_size_mode_exact_height = 0x02,
200+
size_mode_normal = 0x00,
201+
size_mode_exact_width = 0x01,
202+
size_mode_exact_height = 0x02,
203+
size_mode_content = 0x04,
203204
};
204205

205206
struct typed_int
@@ -233,13 +234,11 @@ namespace litehtml
233234
};
234235

235236
typed_int width; // width of the containing block
236-
bool width_is_flex_basis;
237237
typed_int render_width;
238238
typed_int min_width;
239239
typed_int max_width;
240240

241241
typed_int height; // height of the containing block
242-
bool height_is_flex_basis;
243242
typed_int min_height;
244243
typed_int max_height;
245244

@@ -254,12 +253,11 @@ namespace litehtml
254253
height(0, cbc_value_type_auto),
255254
min_height(0, cbc_value_type_none),
256255
max_height(0, cbc_value_type_none),
257-
context_idx(0), width_is_flex_basis(false),
258-
height_is_flex_basis(false),
259-
size_mode(cbc_size_mode_normal)
256+
context_idx(0),
257+
size_mode(size_mode_normal)
260258
{}
261259

262-
containing_block_context new_width(int w, uint32_t _size_mode = cbc_size_mode_normal) const
260+
containing_block_context new_width(int w, uint32_t _size_mode = size_mode_normal) const
263261
{
264262
containing_block_context ret = *this;
265263
ret.render_width = w - (ret.width - ret.render_width);
@@ -268,7 +266,7 @@ namespace litehtml
268266
return ret;
269267
}
270268

271-
containing_block_context new_width_height(int w, int h, uint32_t _size_mode = cbc_size_mode_normal) const
269+
containing_block_context new_width_height(int w, int h, uint32_t _size_mode = size_mode_normal) const
272270
{
273271
containing_block_context ret = *this;
274272
ret.render_width = w - (ret.width - ret.render_width);

src/element.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,14 @@ element::ptr element::_add_before_after(int type, const style& style)
274274

275275
bool element::is_block_formatting_context() const
276276
{
277+
if(m_css.get_display() == display_block)
278+
{
279+
auto par = parent();
280+
if(par && (par->css().get_display() == display_inline_flex || par->css().get_display() == display_flex))
281+
{
282+
return true;
283+
}
284+
}
277285
if( m_css.get_display() == display_inline_block ||
278286
m_css.get_display() == display_table_cell ||
279287
m_css.get_display() == display_inline_flex ||

src/render_block.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ int litehtml::render_item_block::_render(int x, int y, const containing_block_co
208208
bool requires_rerender = false; // when true, the second pass for content rendering is required
209209

210210
// Set block width
211-
if(!self_size.width_is_flex_basis)
211+
if(!(containing_block_size.size_mode & containing_block_context::size_mode_content))
212212
{
213213
if(self_size.width.type == containing_block_context::cbc_value_type_absolute)
214214
{
@@ -219,12 +219,10 @@ int litehtml::render_item_block::_render(int x, int y, const containing_block_co
219219
}
220220
} else
221221
{
222-
if(ret_width > self_size.render_width)
222+
m_pos.width = ret_width;
223+
if(self_size.width.type == containing_block_context::cbc_value_type_absolute && ret_width > self_size.width)
223224
{
224-
m_pos.width = ret_width;
225-
} else
226-
{
227-
m_pos.width = self_size.render_width;
225+
ret_width = self_size.width;
228226
}
229227
}
230228

@@ -263,25 +261,36 @@ int litehtml::render_item_block::_render(int x, int y, const containing_block_co
263261
}
264262

265263
// Set block height
266-
if (self_size.height.type != containing_block_context::cbc_value_type_auto)
264+
if (self_size.height.type != containing_block_context::cbc_value_type_auto &&
265+
!(containing_block_size.size_mode & containing_block_context::size_mode_content))
267266
{
268-
if(self_size.height > 0)
267+
if (self_size.height > 0)
269268
{
270269
m_pos.height = self_size.height;
271270
}
272-
if(src_el()->css().get_box_sizing() == box_sizing_border_box)
271+
if (src_el()->css().get_box_sizing() == box_sizing_border_box)
273272
{
274273
m_pos.height -= box_sizing_height();
275274
}
276275
} else if (src_el()->is_block_formatting_context())
277-
{
276+
{
278277
// add the floats' height to the block height
279-
int floats_height = fmt_ctx->get_floats_height();
280-
if (floats_height > m_pos.height)
281-
{
282-
m_pos.height = floats_height;
283-
}
284-
}
278+
int floats_height = fmt_ctx->get_floats_height();
279+
if (floats_height > m_pos.height)
280+
{
281+
m_pos.height = floats_height;
282+
}
283+
}
284+
if(containing_block_size.size_mode & containing_block_context::size_mode_content)
285+
{
286+
if(self_size.height.type == containing_block_context::cbc_value_type_absolute)
287+
{
288+
if(self_size.height > m_pos.height)
289+
{
290+
m_pos.height = self_size.height;
291+
}
292+
}
293+
}
285294

286295
// Fix height with min-height attribute
287296
if(self_size.min_height.type != containing_block_context::cbc_value_type_none)

src/render_flex.cpp

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ int litehtml::render_item_flex::_render_content(int x, int y, bool second_pass,
294294

295295
item.el->render(el_x,
296296
el_y,
297-
self_size.new_width(item.main_size - item.el->content_offset_width(), containing_block_context::cbc_size_mode_exact_width), fmt_ctx, false);
297+
self_size.new_width(item.main_size - item.el->content_offset_width(), containing_block_context::size_mode_exact_width), fmt_ctx, false);
298298
ln.cross_size = std::max(ln.cross_size, item.el->height());
299299
el_x += item.el->width();
300300
}
@@ -313,8 +313,8 @@ int litehtml::render_item_flex::_render_content(int x, int y, bool second_pass,
313313
el_y,
314314
self_size.new_width_height(el_ret_width - item.el->content_offset_width(),
315315
item.main_size - item.el->content_offset_height(),
316-
containing_block_context::cbc_size_mode_exact_width |
317-
containing_block_context::cbc_size_mode_exact_height),
316+
containing_block_context::size_mode_exact_width |
317+
containing_block_context::size_mode_exact_height),
318318
fmt_ctx, false);
319319
ln.cross_size = std::max(ln.cross_size, item.el->width());
320320
el_y += item.el->height();
@@ -522,16 +522,16 @@ int litehtml::render_item_flex::_render_content(int x, int y, bool second_pass,
522522
item.el->pos().y - item.el->content_offset_top(),
523523
self_size.new_width_height(ln.cross_size,
524524
item.main_size - item.el->content_offset_height(),
525-
containing_block_context::cbc_size_mode_exact_height),
525+
containing_block_context::size_mode_exact_height),
526526
fmt_ctx, false);
527527
} else
528528
{
529529
item.el->render(el_x,
530530
item.el->pos().y - item.el->content_offset_top(),
531531
self_size.new_width_height(ln.cross_size - item.el->content_offset_width(),
532532
item.main_size - item.el->content_offset_height(),
533-
containing_block_context::cbc_size_mode_exact_width |
534-
containing_block_context::cbc_size_mode_exact_height),
533+
containing_block_context::size_mode_exact_width |
534+
containing_block_context::size_mode_exact_height),
535535
fmt_ctx, false);
536536
}
537537
if(!item.el->css().get_width().is_predefined() && is_wrap_reverse)
@@ -735,7 +735,9 @@ std::list<litehtml::render_item_flex::flex_line> litehtml::render_item_flex::get
735735
{
736736
if (item.el->css().get_min_width().is_predefined())
737737
{
738-
item.min_size = el->render(0, 0, self_size.new_width(el->content_offset_width()), fmt_ctx);
738+
item.min_size = el->render(0, 0,
739+
self_size.new_width(el->content_offset_width(),
740+
containing_block_context::size_mode_content), fmt_ctx);
739741
} else
740742
{
741743
item.min_size = item.el->css().get_min_width().calc_percent(self_size.render_width) +
@@ -749,9 +751,22 @@ std::list<litehtml::render_item_flex::flex_line> litehtml::render_item_flex::get
749751
item.max_size = item.el->css().get_max_width().calc_percent(self_size.render_width) +
750752
el->content_offset_width();
751753
}
752-
if (item.el->css().get_flex_basis().is_predefined())
754+
bool flex_basis_predefined = item.el->css().get_flex_basis().is_predefined();
755+
int predef = flex_basis_auto;
756+
if(flex_basis_predefined)
753757
{
754-
switch (item.el->css().get_flex_basis().predef())
758+
predef = item.el->css().get_flex_basis().predef();
759+
} else
760+
{
761+
if(item.el->css().get_flex_basis().val() < 0)
762+
{
763+
flex_basis_predefined = true;
764+
}
765+
}
766+
767+
if (flex_basis_predefined)
768+
{
769+
switch (predef)
755770
{
756771
case flex_basis_auto:
757772
if (!item.el->css().get_width().is_predefined())
@@ -767,6 +782,9 @@ std::list<litehtml::render_item_flex::flex_line> litehtml::render_item_flex::get
767782
case flex_basis_min_content:
768783
item.base_size = item.min_size;
769784
break;
785+
default:
786+
item.base_size = 0;
787+
break;
770788
}
771789
} else
772790
{
@@ -778,7 +796,7 @@ std::list<litehtml::render_item_flex::flex_line> litehtml::render_item_flex::get
778796
{
779797
if (item.el->css().get_min_height().is_predefined())
780798
{
781-
el->render(0, 0, self_size.new_width(self_size.render_width), fmt_ctx);
799+
el->render(0, 0, self_size.new_width(self_size.render_width, containing_block_context::size_mode_content), fmt_ctx);
782800
item.min_size = el->height();
783801
} else
784802
{
@@ -794,9 +812,22 @@ std::list<litehtml::render_item_flex::flex_line> litehtml::render_item_flex::get
794812
el->content_offset_width();
795813
}
796814

797-
if (item.el->css().get_flex_basis().is_predefined())
815+
bool flex_basis_predefined = item.el->css().get_flex_basis().is_predefined();
816+
int predef = flex_basis_auto;
817+
if(flex_basis_predefined)
798818
{
799-
switch (item.el->css().get_flex_basis().predef())
819+
predef = item.el->css().get_flex_basis().predef();
820+
} else
821+
{
822+
if(item.el->css().get_flex_basis().val() < 0)
823+
{
824+
flex_basis_predefined = true;
825+
}
826+
}
827+
828+
if (flex_basis_predefined)
829+
{
830+
switch (predef)
800831
{
801832
case flex_basis_auto:
802833
if (!item.el->css().get_height().is_predefined())
@@ -813,6 +844,8 @@ std::list<litehtml::render_item_flex::flex_line> litehtml::render_item_flex::get
813844
case flex_basis_min_content:
814845
item.base_size = item.min_size;
815846
break;
847+
default:
848+
item.base_size = 0;
816849
}
817850
} else
818851
{

src/render_item.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ litehtml::containing_block_context litehtml::render_item::calculate_containing_b
10401040
if (src_el()->css().get_display() != display_table_cell)
10411041
{
10421042
auto par = parent();
1043-
if(cb_context.size_mode & containing_block_context::cbc_size_mode_exact_width)
1043+
if(cb_context.size_mode & containing_block_context::size_mode_exact_width)
10441044
{
10451045
ret.width.value = cb_context.width;
10461046
ret.width.type = containing_block_context::cbc_value_type_absolute;
@@ -1064,7 +1064,7 @@ litehtml::containing_block_context litehtml::render_item::calculate_containing_b
10641064
calc_cb_length(*width, cb_context.width, ret.width);
10651065
}
10661066
}
1067-
if(cb_context.size_mode & containing_block_context::cbc_size_mode_exact_height)
1067+
if(cb_context.size_mode & containing_block_context::size_mode_exact_height)
10681068
{
10691069
ret.height.value = cb_context.height;
10701070
ret.height.type = containing_block_context::cbc_value_type_absolute;
-10 Bytes
Loading
399 Bytes
Loading

0 commit comments

Comments
 (0)