Skip to content

Commit ec2d946

Browse files
committed
fixed rendering of <html> and <body> tags
* Removed "width: 100%; height: 100%" from <html> style * extend <html> and <body> elements to the client rectangle if width/height is auto * Reverse inherit for <html> background is working again * Always draw <html> element background into document clipping area to fill entire "browser window". * Added document::content_width and document::content_height. These methodts return the document size without <html> and <body> tags for using in tests and other applications
1 parent 3ba3974 commit ec2d946

File tree

9 files changed

+67
-19
lines changed

9 files changed

+67
-19
lines changed

include/litehtml/background.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ namespace litehtml
2222
int_vector m_repeat;
2323
int_vector m_clip;
2424
int_vector m_origin;
25+
26+
bool is_empty() const
27+
{
28+
if(m_color.alpha != 0) return false;
29+
if(m_image.empty()) return true;
30+
for(const auto& img : m_image)
31+
{
32+
if(!img.empty()) return false;
33+
}
34+
return true;
35+
}
2536
};
2637

2738
class background_paint

include/litehtml/document.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ namespace litehtml
6262
litehtml::css m_master_css;
6363
litehtml::css m_user_css;
6464
litehtml::size m_size;
65+
litehtml::size m_content_size;
6566
position::vector m_fixed_boxes;
6667
media_query_list::vector m_media_lists;
6768
element::ptr m_over_element;
@@ -83,6 +84,8 @@ namespace litehtml
8384
int to_pixels(const css_length& val, int fontSize, int size = 0) const;
8485
int width() const;
8586
int height() const;
87+
int content_width() const;
88+
int content_height() const;
8689
void add_stylesheet(const char* str, const char* baseurl, const char* media);
8790
bool on_mouse_over(int x, int y, int client_x, int client_y, position::vector& redraw_boxes);
8891
bool on_lbutton_down(int x, int y, int client_x, int client_y, position::vector& redraw_boxes);

include/litehtml/master_css.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ namespace litehtml{ const char* const master_css = R"##(
55
66
html {
77
display: block;
8-
height:100%;
9-
width:100%;
108
position: relative;
119
}
1210

include/litehtml/render_item.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ namespace litehtml
325325
void render_positioned(render_type rt = render_all);
326326
void add_positioned(const std::shared_ptr<litehtml::render_item> &el);
327327
void get_redraw_box(litehtml::position& pos, int x = 0, int y = 0);
328-
void calc_document_size( litehtml::size& sz, int x = 0, int y = 0 );
328+
void calc_document_size( litehtml::size& sz, litehtml::size& content_size, int x = 0, int y = 0 );
329329
virtual void get_inline_boxes( position::vector& boxes ) const {};
330330
virtual void set_inline_boxes( position::vector& boxes ) {};
331331
virtual void add_inline_box( const position& box ) {};

src/document.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ int litehtml::document::render( int max_width, render_type rt )
312312
}
313313
m_size.width = 0;
314314
m_size.height = 0;
315-
m_root_render->calc_document_size(m_size);
315+
m_content_size.width = 0;
316+
m_content_size.height = 0;
317+
m_root_render->calc_document_size(m_size, m_content_size);
316318
}
317319
}
318320
return ret;
@@ -431,6 +433,17 @@ int litehtml::document::height() const
431433
return m_size.height;
432434
}
433435

436+
int litehtml::document::content_width() const
437+
{
438+
return m_content_size.width;
439+
}
440+
441+
int litehtml::document::content_height() const
442+
{
443+
return m_content_size.height;
444+
}
445+
446+
434447
void litehtml::document::add_stylesheet( const char* str, const char* baseurl, const char* media )
435448
{
436449
if(str && str[0])

src/html_tag.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ void litehtml::html_tag::draw_background(uint_ptr hdc, int x, int y, const posit
880880

881881
if(m_css.get_display() != display_inline && m_css.get_display() != display_table_row)
882882
{
883-
if(el_pos.does_intersect(clip))
883+
if(el_pos.does_intersect(clip) || is_root())
884884
{
885885
auto v_offset = ri->get_draw_vertical_offset();
886886
pos.y += v_offset;
@@ -891,6 +891,14 @@ void litehtml::html_tag::draw_background(uint_ptr hdc, int x, int y, const posit
891891
{
892892
std::vector<background_paint> bg_paint;
893893
init_background_paint(pos, bg_paint, bg, ri);
894+
if(is_root())
895+
{
896+
for(auto& b : bg_paint)
897+
{
898+
b.clip_box = *clip;
899+
b.border_box = *clip;
900+
}
901+
}
894902

895903
get_document()->container()->draw_background(hdc, bg_paint);
896904
}
@@ -1621,14 +1629,14 @@ const litehtml::background* litehtml::html_tag::get_background(bool own_only)
16211629
if(own_only)
16221630
{
16231631
// return own background with check for empty one
1624-
if(m_css.get_bg().m_image.empty() && !m_css.get_bg().m_color.alpha)
1632+
if(m_css.get_bg().is_empty())
16251633
{
16261634
return nullptr;
16271635
}
16281636
return &m_css.get_bg();
16291637
}
16301638

1631-
if(m_css.get_bg().m_image.empty() && !m_css.get_bg().m_color.alpha)
1639+
if(m_css.get_bg().is_empty())
16321640
{
16331641
// if this is root element (<html>) try to get background from body
16341642
if (is_root())

src/render_block.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,18 @@ int litehtml::render_item_block::render(int x, int y, const containing_block_con
799799
_render_content(x, y, true, ret_width, self_size.new_width(m_pos.width));
800800
}
801801

802+
if(src_el()->is_root() || src_el()->is_body())
803+
{
804+
if(self_size.width.type == containing_block_context::cbc_value_type_auto && m_pos.width < containing_block_size.width - content_offset_width())
805+
{
806+
m_pos.width = containing_block_size.width - content_offset_width();
807+
}
808+
if(self_size.height.type == containing_block_context::cbc_value_type_auto && m_pos.height < containing_block_size.height - content_offset_height())
809+
{
810+
m_pos.height = containing_block_size.height - content_offset_height();
811+
}
812+
}
813+
802814
if (src_el()->is_floats_holder() && !second_pass)
803815
{
804816
for (const auto& fb : m_floats_left)

src/render_item.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -504,14 +504,17 @@ void litehtml::render_item::get_redraw_box(litehtml::position& pos, int x /*= 0*
504504
}
505505
}
506506

507-
void litehtml::render_item::calc_document_size( litehtml::size& sz, int x /*= 0*/, int y /*= 0*/ )
507+
void litehtml::render_item::calc_document_size( litehtml::size& sz, litehtml::size& content_size, int x /*= 0*/, int y /*= 0*/ )
508508
{
509509
if(is_visible() && src_el()->css().get_position() != element_position_fixed)
510510
{
511-
if(have_parent() && !src_el()->is_body())
511+
sz.width = std::max(sz.width, x + right());
512+
sz.height = std::max(sz.height, y + bottom());
513+
514+
if(!src_el()->is_root() && !src_el()->is_body())
512515
{
513-
sz.width = std::max(sz.width, x + right());
514-
sz.height = std::max(sz.height, y + bottom());
516+
content_size.width = std::max(content_size.width, x + right());
517+
content_size.height = std::max(content_size.height, y + bottom());
515518
}
516519

517520
// All children of tables and blocks with style other than "overflow: visible" are inside element.
@@ -520,14 +523,14 @@ void litehtml::render_item::calc_document_size( litehtml::size& sz, int x /*= 0*
520523
{
521524
for(auto& el : m_children)
522525
{
523-
el->calc_document_size(sz, x + m_pos.x, y + m_pos.y);
526+
el->calc_document_size(sz, content_size, x + m_pos.x, y + m_pos.y);
524527
}
525528
}
526529

527-
if(!have_parent() || src_el()->is_body())
530+
if(src_el()->is_root() || src_el()->is_body())
528531
{
529-
sz.width += content_offset_right();
530-
sz.height += content_offset_bottom();
532+
content_size.width += content_offset_right();
533+
content_size.height += content_offset_bottom();
531534
}
532535
}
533536
}
@@ -1007,11 +1010,11 @@ litehtml::containing_block_context litehtml::render_item::calculate_containing_b
10071010
{
10081011
calc_cb_length(src_el()->css().get_width(), cb_context.width, ret.width);
10091012
calc_cb_length(src_el()->css().get_height(), cb_context.height, ret.height);
1010-
if (src_el()->css().get_display() == display_table && ret.width.type != containing_block_context::cbc_value_type_auto || src_el()->is_root())
1013+
if (ret.width.type != containing_block_context::cbc_value_type_auto && (src_el()->css().get_display() == display_table || src_el()->is_root()))
10111014
{
10121015
ret.width.value -= content_offset_width();
10131016
}
1014-
if (src_el()->css().get_display() == display_table && ret.height.type != containing_block_context::cbc_value_type_auto || src_el()->is_root())
1017+
if (ret.height.type != containing_block_context::cbc_value_type_auto && (src_el()->css().get_display() == display_table || src_el()->is_root()))
10151018
{
10161019
ret.height.value -= content_offset_height();
10171020
}

test/render_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ void test(string filename)
6868
{
6969
string html = readfile(filename);
7070

71-
int width = 800, height = 1600; // image will be cropped to contain only the "inked" part
71+
int width = 800, height = 1600; // image will be cropped to content_width/content_height
7272
test_container container(width, height, test_dir);
7373

7474
auto doc = document::createFromString(html.c_str(), &container);
7575
doc->render(width);
76-
Bitmap bmp = draw(doc, doc->width(), doc->height());
76+
Bitmap bmp = draw(doc, doc->content_width(), doc->content_height());
7777

7878
Bitmap good(filename + ".png");
7979
if (bmp != good)

0 commit comments

Comments
 (0)