Skip to content
Permalink
Browse files
Keep br while replacing plain texts with content ending with newline
Keep `br` while replacing plain texts with content ending with newline
https://bugs.webkit.org/show_bug.cgi?id=90206

Reviewed by Ryosuke Niwa.

Merge - https://src.chromium.org/viewvc/blink?view=revision&revision=165565

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: https://commits.webkit.org/257205@main
  • 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

TEST COMPLETE

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

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

// copy a new line
textarea.setSelectionRange(99, 100);
document.execCommand('Copy');

document.execCommand('SelectAll');
document.execCommand('Paste');
shouldBeEqualToString('textarea.value', '\n');
</script>
</body>
</html>
@@ -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() };
insertedNodes.willRemoveNode(endBR.get());
removeNode(*endBR);

0 comments on commit a4612d7

Please sign in to comment.