1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:17:35 +00:00

LibWeb: Make whitespace collapsing stateless

Previously, the whitespace collapsing code had a parameter telling it
whether the previous text node ended in whitespace. This was not
actually necessary, so let's get rid of it.
This commit is contained in:
Andreas Kling 2022-03-27 21:14:10 +02:00
parent 66582a875f
commit 4575ab558b
4 changed files with 10 additions and 19 deletions

View file

@ -123,11 +123,8 @@ Optional<InlineLevelIterator::Item> InlineLevelIterator::next(float available_wi
if (is<Layout::TextNode>(*m_current_node)) { if (is<Layout::TextNode>(*m_current_node)) {
auto& text_node = static_cast<Layout::TextNode const&>(*m_current_node); auto& text_node = static_cast<Layout::TextNode const&>(*m_current_node);
if (!m_text_node_context.has_value()) { if (!m_text_node_context.has_value())
auto& line_boxes = m_formatting_state.get(m_container).line_boxes; enter_text_node(text_node);
bool previous_is_empty_or_ends_in_whitespace = line_boxes.is_empty() || line_boxes.last().is_empty_or_ends_in_whitespace();
enter_text_node(text_node, previous_is_empty_or_ends_in_whitespace);
}
auto chunk_opt = m_text_node_context->next_chunk; auto chunk_opt = m_text_node_context->next_chunk;
if (!chunk_opt.has_value()) { if (!chunk_opt.has_value()) {
@ -225,7 +222,7 @@ Optional<InlineLevelIterator::Item> InlineLevelIterator::next(float available_wi
return item; return item;
} }
void InlineLevelIterator::enter_text_node(Layout::TextNode const& text_node, bool previous_is_empty_or_ends_in_whitespace) void InlineLevelIterator::enter_text_node(Layout::TextNode const& text_node)
{ {
bool do_collapse = true; bool do_collapse = true;
bool do_wrap_lines = true; bool do_wrap_lines = true;
@ -250,7 +247,7 @@ void InlineLevelIterator::enter_text_node(Layout::TextNode const& text_node, boo
} }
// FIXME: The const_cast here is gross. // FIXME: The const_cast here is gross.
const_cast<TextNode&>(text_node).compute_text_for_rendering(do_collapse, previous_is_empty_or_ends_in_whitespace); const_cast<TextNode&>(text_node).compute_text_for_rendering(do_collapse);
m_text_node_context = TextNodeContext { m_text_node_context = TextNodeContext {
.do_collapse = do_collapse, .do_collapse = do_collapse,

View file

@ -56,7 +56,7 @@ private:
void skip_to_next(); void skip_to_next();
void compute_next(); void compute_next();
void enter_text_node(Layout::TextNode const&, bool previous_is_empty_or_ends_in_whitespace); void enter_text_node(Layout::TextNode const&);
void enter_node_with_box_model_metrics(Layout::NodeWithStyleAndBoxModelMetrics const&); void enter_node_with_box_model_metrics(Layout::NodeWithStyleAndBoxModelMetrics const&);
void exit_node_with_box_model_metrics(); void exit_node_with_box_model_metrics();

View file

@ -32,8 +32,8 @@ static bool is_all_whitespace(StringView string)
return true; return true;
} }
// NOTE: This collapes whitespace into a single ASCII space if collapse is true. If previous_is_empty_or_ends_in_whitespace, it also strips leading whitespace. // NOTE: This collapses whitespace into a single ASCII space if collapse is true.
void TextNode::compute_text_for_rendering(bool collapse, bool previous_is_empty_or_ends_in_whitespace) void TextNode::compute_text_for_rendering(bool collapse)
{ {
auto& data = dom_node().data(); auto& data = dom_node().data();
if (!collapse || data.is_empty()) { if (!collapse || data.is_empty()) {
@ -44,12 +44,8 @@ void TextNode::compute_text_for_rendering(bool collapse, bool previous_is_empty_
// NOTE: A couple fast returns to avoid unnecessarily allocating a StringBuilder. // NOTE: A couple fast returns to avoid unnecessarily allocating a StringBuilder.
if (data.length() == 1) { if (data.length() == 1) {
if (is_ascii_space(data[0])) { if (is_ascii_space(data[0])) {
if (previous_is_empty_or_ends_in_whitespace) static String s_single_space_string = " ";
m_text_for_rendering = String::empty(); m_text_for_rendering = s_single_space_string;
else {
static String s_single_space_string = " ";
m_text_for_rendering = s_single_space_string;
}
} else { } else {
m_text_for_rendering = data; m_text_for_rendering = data;
} }
@ -76,8 +72,6 @@ void TextNode::compute_text_for_rendering(bool collapse, bool previous_is_empty_
++index; ++index;
}; };
if (previous_is_empty_or_ends_in_whitespace)
skip_over_whitespace();
while (index < data.length()) { while (index < data.length()) {
if (is_ascii_space(data[index])) { if (is_ascii_space(data[index])) {
builder.append(' '); builder.append(' ');

View file

@ -46,7 +46,7 @@ public:
Utf8View::Iterator m_iterator; Utf8View::Iterator m_iterator;
}; };
void compute_text_for_rendering(bool collapse, bool previous_is_empty_or_ends_in_whitespace); void compute_text_for_rendering(bool collapse);
virtual RefPtr<Painting::Paintable> create_paintable() const override; virtual RefPtr<Painting::Paintable> create_paintable() const override;