Skip to content
Browse files
Keep br while replacing plain texts with content ending with newline
Keep `br` while replacing plain texts with content ending with newline

Reviewed by Ryosuke Niwa.

Merge -

The <br> after the insert position was removed while inserting a text
ending with a newline. This caused the loss of the newline at the end
of the inserted text. This fix adds extra condition checking for this
scenario and keeps the <br> while replacing plain texts with content
ending with newline.

* Source/WebCore/editing/ReplaceSelectionCommand.cpp:
(ReplaceSelectionCommand::doApply): Update logic to account for 'br' while inserting text with newline
* LayoutTests/editing/pasteboard/paste-newline-in-all-selected-textarea.html: Add Test Case
* LayoutTests/editing/pasteboard/paste-newline-in-all-selected-textarea-expected.txt: Add Test Case Expectation

Canonical link:
  • Loading branch information
Ahmad-S792 authored and Ahmad Saleem committed Dec 1, 2022
1 parent a7b6b0e commit a4612d781cd3a4631a4e3d9875175f03adda2c2c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
@@ -0,0 +1,5 @@
PASS textarea.value is "\n"
PASS successfullyParsed is true


@@ -0,0 +1,23 @@
<!DOCTYPE html>
<textarea id="test" cols=5 rows=4>
This test verifies that pasting a newline in a textarea with all text selected
produces a newline.

<script src="../../resources/js-test.js"></script>
var textarea = document.getElementById('test');

// copy a new line
textarea.setSelectionRange(99, 100);

shouldBeEqualToString('textarea.value', '\n');
@@ -1356,7 +1356,7 @@ void ReplaceSelectionCommand::doApply()
if (!startOfInsertedContent.isNull() && insertionBlock && insertionPos.deprecatedNode() == insertionBlock->parentNode() && (unsigned)insertionPos.deprecatedEditingOffset() < insertionBlock->computeNodeIndex() && !isStartOfParagraph(startOfInsertedContent))
insertNodeAt(HTMLBRElement::create(document()), startOfInsertedContent.deepEquivalent());

if (endBR && (plainTextFragment || shouldRemoveEndBR(endBR.get(), originalVisPosBeforeEndBR))) {
if (endBR && (plainTextFragment || (shouldRemoveEndBR(endBR.get(), originalVisPosBeforeEndBR) && !(fragment.hasInterchangeNewlineAtEnd() && selectionIsPlainText)))) {
RefPtr parent { endBR->parentNode() };

0 comments on commit a4612d7

Please sign in to comment.