Skip to content

LibWeb: Insert newlines between block-level elements when copying text#8905

Open
officialasishkumar wants to merge 1 commit intoLadybirdBrowser:masterfrom
officialasishkumar:clipboard-newline-between-blocks
Open

LibWeb: Insert newlines between block-level elements when copying text#8905
officialasishkumar wants to merge 1 commit intoLadybirdBrowser:masterfrom
officialasishkumar:clipboard-newline-between-blocks

Conversation

@officialasishkumar
Copy link
Copy Markdown

When copying text that spans multiple block-level elements (e.g. adjacent
<div> nodes), the clipboard received all the text concatenated without
any newlines between the elements.

The root cause was in visible_text_in_range(), which iterated the
selection range and only appended data from Text nodes. Element nodes
(including block containers and <br>) were silently skipped, so no
line separators were ever inserted.

Fix: extend the iteration callback to also check element nodes: if
the node's layout representation is a block container (<div>, <p>,
<h1><h6>, etc.) or a BreakNode (<br>), and the output buffer
is non-empty, a newline is inserted. The non-empty guard prevents a
spurious leading newline at the start of the copied text.

This matches the plain-text clipboard serialisation behaviour of
Chrome and Firefox for the common case.

Fixes #7315.

@ladybird-bot
Copy link
Copy Markdown
Collaborator

Hello!

One or more of the commit messages in this PR do not match the Ladybird code submission policy, please check the lint_commits CI job for more details on which commits were flagged and why.
Please do not close this PR and open another, instead modify your commit message(s) with git commit --amend and force push those changes to update this PR.

@officialasishkumar officialasishkumar force-pushed the clipboard-newline-between-blocks branch from 45d680d to b713a4e Compare April 13, 2026 20:28
@trflynn89
Copy link
Copy Markdown
Contributor

trflynn89 commented Apr 13, 2026

@officialasishkumar Please slow down. Opening 10 PRs in a row as a new contributor is too much. Open just one, learn the ropes and contributing guidelines, and go from there.

Spamming this many projects is not a good look:
Screenshot 2026-04-13 at 6 09 06 PM

The visible_text_in_range() helper used for clipboard serialisation only
iterated over Text nodes, ignoring the block-level element boundaries
between them. Selecting text that spans multiple <div> (or other block)
elements would therefore produce a result with all the text concatenated
without any separating newlines.

Fix by also processing Element nodes while iterating the range: if an
element's layout node is a block container or a line-break node (<br>),
and the output buffer already contains some text, append a newline
before continuing. This matches the plain-text serialisation behaviour
of other browsers for the common case.

Fixes LadybirdBrowser#7315.
@officialasishkumar officialasishkumar force-pushed the clipboard-newline-between-blocks branch from b713a4e to 1eaecaa Compare April 13, 2026 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Copy text does not create newlines between div elements

3 participants