1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 19:47:42 +00:00

LibHTML: Don't insert line breaks between multiple <pre>'s on a line

When iterating lines for "white-space: pre", we should only break when
there is an actual line break character.
This commit is contained in:
Andreas Kling 2019-12-18 12:44:09 +01:00
parent 152c31c5c6
commit 4d81bc20d8

View file

@ -107,23 +107,24 @@ void LayoutText::for_each_source_line(Callback callback) const
auto commit_line = [&](auto it) { auto commit_line = [&](auto it) {
int start = view.byte_offset_of(start_of_line); int start = view.byte_offset_of(start_of_line);
int length = view.byte_offset_of(it) - view.byte_offset_of(start_of_line); int length = view.byte_offset_of(it) - view.byte_offset_of(start_of_line);
callback(view.substring_view(start, length), start, length);
if (length > 0) {
callback(view.substring_view(start, length), start, length);
}
}; };
bool last_was_newline = false;
for (auto it = view.begin(); it != view.end();) { for (auto it = view.begin(); it != view.end();) {
bool did_commit = false; bool did_commit = false;
if (*it == '\n') { if (*it == '\n') {
commit_line(it); commit_line(it);
did_commit = true; did_commit = true;
last_was_newline = true;
} else {
last_was_newline = false;
} }
++it; ++it;
if (did_commit) if (did_commit)
start_of_line = it; start_of_line = it;
} }
if (start_of_line != view.end()) if (start_of_line != view.end() || last_was_newline)
commit_line(view.end()); commit_line(view.end());
} }
@ -140,9 +141,14 @@ void LayoutText::split_into_lines(LayoutBlock& container)
bool is_preformatted = style().string_or_fallback(CSS::PropertyID::WhiteSpace, "normal") == "pre"; bool is_preformatted = style().string_or_fallback(CSS::PropertyID::WhiteSpace, "normal") == "pre";
if (is_preformatted) { if (is_preformatted) {
m_text_for_rendering = node().data(); m_text_for_rendering = node().data();
int line_count = 0;
for_each_source_line([&](const Utf8View& view, int start, int length) { for_each_source_line([&](const Utf8View& view, int start, int length) {
if (line_count == 1)
line_boxes.append(LineBox());
line_boxes.last().add_fragment(*this, start, length, font.width(view), font.glyph_height()); line_boxes.last().add_fragment(*this, start, length, font.width(view), font.glyph_height());
line_boxes.append(LineBox()); if (line_count != 0 && line_boxes.last().width() > 0)
line_boxes.append(LineBox());
++line_count;
}); });
return; return;
} }