Skip to content

Commit 714d234

Browse files
committed
fixed support for box-sizing: border-box
1 parent f3b2fc0 commit 714d234

File tree

4 files changed

+110
-33
lines changed

4 files changed

+110
-33
lines changed

include/litehtml/render_item.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,36 @@ namespace litehtml
221221
return content_offset_top() + content_offset_bottom();
222222
}
223223

224+
int box_sizing_left() const
225+
{
226+
return m_padding.left + m_borders.left;
227+
}
228+
229+
int box_sizing_right() const
230+
{
231+
return m_padding.right + m_borders.right;
232+
}
233+
234+
int box_sizing_width() const
235+
{
236+
return box_sizing_left() + border_right();
237+
}
238+
239+
int box_sizing_top() const
240+
{
241+
return m_padding.top + m_borders.top;
242+
}
243+
244+
int box_sizing_bottom() const
245+
{
246+
return m_padding.bottom + m_borders.bottom;
247+
}
248+
249+
int box_sizing_height() const
250+
{
251+
return box_sizing_top() + border_bottom();
252+
}
253+
224254
void parent(const std::shared_ptr<render_item>& par)
225255
{
226256
m_parent = par;

src/render_block.cpp

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -676,58 +676,70 @@ int litehtml::render_item_block::_render(int x, int y, int max_width, const cont
676676

677677
containing_block_context cb_size = calculate_containing_block_context(containing_block_size);
678678

679-
max_width -= content_offset_left() + content_offset_right();
680-
if(max_width < 0) max_width = 0;
681-
max_width = std::min(max_width, cb_size.width);
679+
if(cb_size.width_type != containing_block_context::cbc_value_type_auto)
680+
{
681+
// If width is absolute value, we render into cb_size.width size
682+
max_width = cb_size.width;
683+
if(src_el()->css().get_box_sizing() == box_sizing_border_box)
684+
{
685+
// for style "box-sizing: border-box" we have to decrease rendering width for borders + paddings
686+
max_width -= box_sizing_width();
687+
}
688+
} else
689+
{
690+
// for "width: auto" we render with max_width minus content offset
691+
max_width -= content_offset_width();
692+
if (max_width < 0) max_width = 0;
693+
}
682694

683695
//*****************************************
684696
// Render content
685697
//*****************************************
686698
ret_width = _render_content(x, y, max_width, second_pass, ret_width, cb_size);
687699
//*****************************************
688700

689-
bool requires_rerender = false;
701+
bool requires_rerender = false; // when true, the second pass for content rendering is required
690702

691703
// Set block width
692-
if(cb_size.width_type == containing_block_context::cbc_value_type_auto &&
693-
(src_el()->is_inline_box() ||
694-
src_el()->css().get_float() != float_none ||
695-
src_el()->css().get_display() == display_table_cell ||
696-
src_el()->css().get_display() == display_table_caption ||
697-
src_el()->css().get_position() > element_position_relative
698-
))
704+
if(cb_size.width_type == containing_block_context::cbc_value_type_absolute)
699705
{
700-
m_pos.width = ret_width;
701-
if(ret_width < cb_size.width)
706+
m_pos.width = cb_size.width;
707+
ret_width = cb_size.width;
708+
if(src_el()->css().get_box_sizing() == box_sizing_border_box)
702709
{
703-
// We have to render content again with new max_width
704-
requires_rerender = true;
710+
m_pos.width -= box_sizing_width();
705711
}
706-
} else
712+
} else if(cb_size.width_type == containing_block_context::cbc_value_type_percentage)
707713
{
708714
m_pos.width = cb_size.width;
709-
if(cb_size.width_type == containing_block_context::cbc_value_type_absolute)
715+
if(src_el()->css().get_box_sizing() == box_sizing_border_box)
710716
{
711-
ret_width = cb_size.width;
717+
m_pos.width -= box_sizing_width();
712718
}
713-
}
714-
715-
// Set block height
716-
if (cb_size.height_type != containing_block_context::cbc_value_type_auto)
719+
} else
717720
{
718-
m_pos.height = cb_size.height;
721+
if(src_el()->is_inline_box() ||
722+
src_el()->css().get_float() != float_none ||
723+
src_el()->css().get_display() == display_table_cell ||
724+
src_el()->css().get_display() == display_table_caption ||
725+
src_el()->css().get_position() > element_position_relative)
726+
{
727+
m_pos.width = ret_width;
728+
if(ret_width < cb_size.width)
729+
{
730+
// We have to render content again with new max_width
731+
requires_rerender = true;
732+
}
733+
} else
734+
{
735+
m_pos.width = cb_size.width;
736+
if(src_el()->css().get_box_sizing() == box_sizing_border_box)
737+
{
738+
m_pos.width -= box_sizing_width();
739+
}
740+
}
719741
}
720742

721-
// add the floats' height to the block height
722-
if (src_el()->is_floats_holder())
723-
{
724-
int floats_height = get_floats_height();
725-
if (floats_height > m_pos.height)
726-
{
727-
m_pos.height = floats_height;
728-
}
729-
}
730-
731743
calc_auto_margins(containing_block_size.width);
732744

733745
// Fix width with min-width attribute
@@ -750,6 +762,26 @@ int litehtml::render_item_block::_render(int x, int y, int max_width, const cont
750762
}
751763
}
752764

765+
// Set block height
766+
if (cb_size.height_type != containing_block_context::cbc_value_type_auto)
767+
{
768+
m_pos.height = cb_size.height;
769+
if(src_el()->css().get_box_sizing() == box_sizing_border_box)
770+
{
771+
m_pos.height -= box_sizing_height();
772+
}
773+
}
774+
775+
// add the floats' height to the block height
776+
if (src_el()->is_floats_holder())
777+
{
778+
int floats_height = get_floats_height();
779+
if (floats_height > m_pos.height)
780+
{
781+
m_pos.height = floats_height;
782+
}
783+
}
784+
753785
// Fix height with min-height attribute
754786
if(cb_size.min_height_type != containing_block_context::cbc_value_type_none)
755787
{

test/render/test25.htm

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<div style="position: relative; width: 100px; height: 100px; padding: 5px; background-color: red">
2+
<span style="display: block; border: 5px solid green; background-color: lightgreen; width: 100%; height: 100%; box-sizing: border-box">Lorem ipsum dolor sit amets, consectetur.</span>
3+
</div>
4+
5+
<div style="position: relative; height: 100px; padding: 5px; background-color: red">
6+
<span style="display: block; border: 5px solid green; background-color: lightgreen; width: 100%; height: 100%; box-sizing: border-box">Lorem ipsum dolor sit amets, consectetur.</span>
7+
</div>
8+
9+
<div style="position: relative; width: 100px; height: 100px; padding: 5px; background-color: red">
10+
<span style="display: block; border: 5px solid green; background-color: lightgreen; width: 105px; height: 105px; box-sizing: border-box">Lorem ipsum dolor sit amets, consectetur.</span>
11+
</div>
12+
13+
<div style="position: relative; width: 100px; height: 100px; padding: 5px; background-color: blue">
14+
<span style="display: block; border: 5px solid red; background-color: lightgreen; width: 100%; height: 100%; box-sizing: border-box; margin: 10px">Lorem ipsum dolor sit amets, consectetur.</span>
15+
</div>

test/render/test25.htm.png

2.63 KB
Loading

0 commit comments

Comments
 (0)