Skip to content

Commit

Permalink
LibWeb: Paint the text caret for elements with no text
Browse files Browse the repository at this point in the history
Previously, no caret was painted if the relevant text node was empty
because no text fragment was created. An empty fragment is now created
if the DOM node associated with an empty text node is editable.
  • Loading branch information
tcl3 committed Jan 15, 2025
1 parent b43bb24 commit 31b2299
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
19 changes: 18 additions & 1 deletion Libraries/LibWeb/Layout/TextNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,10 @@ Unicode::Segmenter& TextNode::grapheme_segmenter() const
return *m_grapheme_segmenter;
}

TextNode::ChunkIterator::ChunkIterator(TextNode const& text_node, bool wrap_lines, bool respect_linebreaks)
TextNode::ChunkIterator::ChunkIterator(TextNode const& text_node, bool wrap_lines, bool respect_linebreaks, bool may_contain_caret)
: m_wrap_lines(wrap_lines)
, m_respect_linebreaks(respect_linebreaks)
, may_contain_caret(may_contain_caret)
, m_utf8_view(text_node.text_for_rendering())
, m_font_cascade_list(text_node.computed_values().font_list())
, m_grapheme_segmenter(text_node.grapheme_segmenter())
Expand Down Expand Up @@ -481,6 +482,22 @@ Optional<TextNode::Chunk> TextNode::ChunkIterator::peek(size_t count)

Optional<TextNode::Chunk> TextNode::ChunkIterator::next_without_peek()
{

if (may_contain_caret && m_utf8_view.is_empty() && m_current_index == m_utf8_view.byte_length()) {
auto const& font = m_font_cascade_list.first();
m_current_index++;
return Chunk {
.view = {},
.font = font,
.start = 0,
.length = 0,
.has_breaking_newline = false,
.has_breaking_tab = false,
.is_all_whitespace = false,
.text_type = Gfx::GlyphRun::TextType::Common,
};
}

if (m_current_index >= m_utf8_view.byte_length())
return {};

Expand Down
3 changes: 2 additions & 1 deletion Libraries/LibWeb/Layout/TextNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class TextNode final : public Node {

class ChunkIterator {
public:
ChunkIterator(TextNode const&, bool wrap_lines, bool respect_linebreaks);
ChunkIterator(TextNode const&, bool wrap_lines, bool respect_linebreaks, bool may_contain_caret);

Optional<Chunk> next();
Optional<Chunk> peek(size_t);
Expand All @@ -51,6 +51,7 @@ class TextNode final : public Node {

bool const m_wrap_lines;
bool const m_respect_linebreaks;
bool const may_contain_caret;
Utf8View m_utf8_view;
Gfx::FontCascadeList const& m_font_cascade_list;

Expand Down

0 comments on commit 31b2299

Please sign in to comment.