1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 21:37:34 +00:00

LibHTML: Insert a new linebox for every newline in "white-space: pre"

This commit is contained in:
Andreas Kling 2019-12-18 12:44:16 +01:00
parent 1fda417f4f
commit 0d6f186397
2 changed files with 13 additions and 18 deletions

View file

@ -95,9 +95,12 @@ void LayoutText::for_each_word(Callback callback) const
commit_word(view.end()); commit_word(view.end());
} }
template<typename Callback> void LayoutText::split_preformatted_into_lines(LayoutBlock& container)
void LayoutText::for_each_source_line(Callback callback) const
{ {
auto& font = style().font();
auto& line_boxes = container.line_boxes();
m_text_for_rendering = node().data();
Utf8View view(m_text_for_rendering); Utf8View view(m_text_for_rendering);
if (view.is_empty()) if (view.is_empty())
return; return;
@ -107,7 +110,8 @@ 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)
line_boxes.last().add_fragment(*this, start, length, font.width(view), font.glyph_height());
}; };
bool last_was_newline = false; bool last_was_newline = false;
@ -115,6 +119,7 @@ void LayoutText::for_each_source_line(Callback callback) const
bool did_commit = false; bool did_commit = false;
if (*it == '\n') { if (*it == '\n') {
commit_line(it); commit_line(it);
line_boxes.append(LineBox());
did_commit = true; did_commit = true;
last_was_newline = true; last_was_newline = true;
} else { } else {
@ -138,20 +143,10 @@ void LayoutText::split_into_lines(LayoutBlock& container)
line_boxes.append(LineBox()); line_boxes.append(LineBox());
float available_width = container.width() - line_boxes.last().width(); float available_width = container.width() - line_boxes.last().width();
bool is_preformatted = style().string_or_fallback(CSS::PropertyID::WhiteSpace, "normal") == "pre"; if (style().string_or_fallback(CSS::PropertyID::WhiteSpace, "normal") == "pre") {
if (is_preformatted) { split_preformatted_into_lines(container);
m_text_for_rendering = node().data();
int line_count = 0;
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());
if (line_count != 0 && line_boxes.last().width() > 0)
line_boxes.append(LineBox());
++line_count;
});
return; return;
} }
// Collapse whitespace into single spaces // Collapse whitespace into single spaces
auto utf8_view = Utf8View(node().data()); auto utf8_view = Utf8View(node().data());

View file

@ -25,10 +25,10 @@ public:
const StyleProperties& style() const { return parent()->style(); } const StyleProperties& style() const { return parent()->style(); }
private: private:
void split_preformatted_into_lines(LayoutBlock& container);
template<typename Callback> template<typename Callback>
void for_each_word(Callback) const; void for_each_word(Callback) const;
template<typename Callback>
void for_each_source_line(Callback) const;
String m_text_for_rendering; String m_text_for_rendering;
}; };