Skip to content

Commit 36ce975

Browse files
committed
Support for "position=relative" for floating boxes
1 parent f2b5e9d commit 36ce975

File tree

4 files changed

+59
-78
lines changed

4 files changed

+59
-78
lines changed

src/box.cpp

Lines changed: 2 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,7 @@ void litehtml::block_box::add_element(const element::ptr &el)
2727
void litehtml::block_box::finish(bool last_box)
2828
{
2929
if(!m_element) return;
30-
31-
css_offsets offsets;
32-
if(m_element->get_element_position(&offsets) == element_position_relative)
33-
{
34-
if(!offsets.left.is_predefined())
35-
{
36-
m_element->m_pos.x += offsets.left.calc_percent(m_box_right - m_box_left);
37-
} else if(!offsets.right.is_predefined())
38-
{
39-
m_element->m_pos.x -= offsets.right.calc_percent(m_box_right - m_box_left);
40-
}
41-
if(!offsets.top.is_predefined())
42-
{
43-
int h = 0;
44-
if(offsets.top.units() == css_units_percentage)
45-
{
46-
element::ptr el_parent = m_element->parent();
47-
if(el_parent)
48-
{
49-
el_parent->get_predefined_height(h);
50-
}
51-
}
52-
m_element->m_pos.y += offsets.top.calc_percent(h);
53-
} else if(!offsets.bottom.is_predefined())
54-
{
55-
int h = 0;
56-
if(offsets.bottom.units() == css_units_percentage)
57-
{
58-
element::ptr el_parent = m_element->parent();
59-
if (el_parent)
60-
{
61-
el_parent->get_predefined_height(h);
62-
}
63-
}
64-
m_element->m_pos.y -= offsets.bottom.calc_percent(h);
65-
}
66-
}
30+
m_element->apply_relative_shift(m_box_right - m_box_left);
6731
}
6832

6933
bool litehtml::block_box::can_hold(const element::ptr &el, white_space ws)
@@ -325,47 +289,7 @@ void litehtml::line_box::finish(bool last_box)
325289
}
326290
}
327291

328-
// update position for relative positioned elements
329-
330-
if(el->get_element_position(&offsets) == element_position_relative)
331-
{
332-
if(!offsets.left.is_predefined())
333-
{
334-
el->m_pos.x += offsets.left.calc_percent(m_box_right - m_box_left);
335-
} else if(!offsets.right.is_predefined())
336-
{
337-
el->m_pos.x -= offsets.right.calc_percent(m_box_right - m_box_left);
338-
}
339-
if(!offsets.top.is_predefined())
340-
{
341-
int h = 0;
342-
343-
if(offsets.top.units() == css_units_percentage)
344-
{
345-
element::ptr el_parent = m_items.back()->parent();
346-
if(el_parent)
347-
{
348-
el_parent->get_predefined_height(h);
349-
}
350-
}
351-
352-
el->m_pos.y += offsets.top.calc_percent(h);
353-
} else if(!offsets.bottom.is_predefined())
354-
{
355-
int h = 0;
356-
357-
if(offsets.top.units() == css_units_percentage)
358-
{
359-
element::ptr el_parent = m_items.back()->parent();
360-
if (el_parent)
361-
{
362-
el_parent->get_predefined_height(h);
363-
}
364-
}
365-
366-
el->m_pos.y -= offsets.bottom.calc_percent(h);
367-
}
368-
}
292+
el->apply_relative_shift(m_box_right - m_box_left);
369293
}
370294
m_height = y2 - y1;
371295
m_baseline = (base_line - y1) - (m_height - line_height);

src/element.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,53 @@ int litehtml::element::get_inline_shift_right()
263263
return ret;
264264
}
265265

266+
void litehtml::element::apply_relative_shift(int parent_width)
267+
{
268+
css_offsets offsets;
269+
if (get_element_position(&offsets) == element_position_relative)
270+
{
271+
element::ptr parent_ptr = parent();
272+
if (!offsets.left.is_predefined())
273+
{
274+
m_pos.x += offsets.left.calc_percent(parent_width);
275+
}
276+
else if (!offsets.right.is_predefined())
277+
{
278+
m_pos.x -= offsets.right.calc_percent(parent_width);
279+
}
280+
if (!offsets.top.is_predefined())
281+
{
282+
int h = 0;
283+
284+
if (offsets.top.units() == css_units_percentage)
285+
{
286+
element::ptr el_parent = parent();
287+
if (el_parent)
288+
{
289+
el_parent->get_predefined_height(h);
290+
}
291+
}
292+
293+
m_pos.y += offsets.top.calc_percent(h);
294+
}
295+
else if (!offsets.bottom.is_predefined())
296+
{
297+
int h = 0;
298+
299+
if (offsets.top.units() == css_units_percentage)
300+
{
301+
element::ptr el_parent = parent();
302+
if (el_parent)
303+
{
304+
el_parent->get_predefined_height(h);
305+
}
306+
}
307+
308+
m_pos.y -= offsets.bottom.calc_percent(h);
309+
}
310+
}
311+
}
312+
266313
void litehtml::element::calc_auto_margins(int parent_width) LITEHTML_EMPTY_FUNC
267314
const litehtml::background* litehtml::element::get_background(bool own_only) LITEHTML_RETURN_FUNC(0)
268315
litehtml::element::ptr litehtml::element::get_element_by_point(int x, int y, int client_x, int client_y) LITEHTML_RETURN_FUNC(0)

src/element.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ namespace litehtml
8585
int calc_width(int defVal) const;
8686
int get_inline_shift_left();
8787
int get_inline_shift_right();
88+
void apply_relative_shift(int parent_width);
8889

8990
std::shared_ptr<document> get_document() const;
9091

src/html_tag.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4175,6 +4175,15 @@ int litehtml::html_tag::render_box(int x, int y, int max_width, bool second_pass
41754175
}
41764176
}
41774177

4178+
if (is_floats_holder() && !second_pass)
4179+
{
4180+
for (const auto& fb : m_floats_left)
4181+
{
4182+
fb.el->apply_relative_shift(fb.el->parent()->calc_width(m_pos.width));
4183+
}
4184+
}
4185+
4186+
41784187
return ret_width;
41794188
}
41804189

0 commit comments

Comments
 (0)