Bug 49288

Summary: REGRESSION (Safari 5?): Pasting a line into textarea inserts two newlines
Product: WebKit Reporter: Alexey Proskuryakov <ap>
Component: HTML EditingAssignee: Ryosuke Niwa <rniwa>
Status: RESOLVED FIXED    
Severity: Normal CC: divya, enrica, marijnh+webkit, rniwa, shezbaig.wk, simon.fraser, tkent, tony, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: 528+ (Nightly build)   
Hardware: Macintosh   
OS: OS X 10.6   
Attachments:
Description Flags
Fixes the bug tony: review+, tony: commit-queue-

Description Alexey Proskuryakov 2010-11-09 17:11:07 PST
Steps to reproduce:
1. Select and copy this whole line in Bugzilla (e.g. via triple-click and Cmd+C).
2. Paste it into additional comments field several times.

There are empty lines between pasted lines, suggesting that two newlines are getting pasted instead of one.

This is rather annoying, I hit this multiple times a day in Bugzilla. This only started to happen time ago - assuming that was Safari 5, but I'm not sure.
Comment 1 Marijn Haverbeke 2012-06-25 11:45:40 PDT
CodeMirror (http://codemirror.net) uses a textarea in a way that makes this bug extra visible. I've had several people report it as a bug in CodeMirror in the past few months [1][2][3], and in fact ran into it writing this very comment in the bug tracker.

This seems a serious, obviously reproduceable bug. I hope it's still on someone's list? (Has been sitting idle in the bug tracker for a long time now.)

[1]: https://github.com/marijnh/CodeMirror2/issues/598
[2]: https://github.com/marijnh/CodeMirror2/issues/474
[3]: http://groups.google.com/group/codemirror/browse_thread/thread/9b42dec8a17a2dc1
Comment 2 Simon Fraser (smfr) 2012-06-25 20:46:37 PDT
This is also important for Adobe's Brackets editor.
Comment 3 Radar WebKit Bug Importer 2012-06-25 20:47:07 PDT
<rdar://problem/11746339>
Comment 4 Ryosuke Niwa 2012-06-26 00:22:19 PDT
Fix:

Index: Source/WebCore/editing/ReplaceSelectionCommand.cpp
===================================================================
--- Source/WebCore/editing/ReplaceSelectionCommand.cpp	(revision 121106)
+++ Source/WebCore/editing/ReplaceSelectionCommand.cpp	(working copy)
@@ -123,9 +123,12 @@
     // The two positions above are the same visual position, but we want to stay in the same block.
     Node* enclosingBlockNode = enclosingBlock(pos.containerNode());
     for (Position nextPosition = pos; nextPosition.containerNode() != enclosingBlockNode; pos = nextPosition) {
+        if (lineBreakExistsAtPosition(pos))
+            break;
+
         if (pos.containerNode()->nonShadowBoundaryParentNode())
             nextPosition = positionInParentAfterNode(pos.containerNode());
-        
+
         if (nextPosition == pos 
             || enclosingBlock(nextPosition.containerNode()) != enclosingBlockNode
             || VisiblePosition(pos) != VisiblePosition(nextPosition))

I'll post a patch tomorrow.
Comment 5 Ryosuke Niwa 2012-06-26 00:49:11 PDT
Maybe caused by http://trac.webkit.org/changeset/55762 ?
Comment 6 Tony Chang 2012-06-26 12:46:48 PDT
(In reply to comment #5)
> Maybe caused by http://trac.webkit.org/changeset/55762 ?

Seems possible. I probably didn't consider the impact on plain text by moving to an equivalent visual position.
Comment 7 Ryosuke Niwa 2012-06-27 02:28:05 PDT
Created attachment 149714 [details]
Fixes the bug
Comment 8 Marijn Haverbeke 2012-06-27 02:33:17 PDT
Thank you for figuring this out!
Comment 9 Tony Chang 2012-06-27 10:35:01 PDT
Comment on attachment 149714 [details]
Fixes the bug

View in context: https://bugs.webkit.org/attachment.cgi?id=149714&action=review

> LayoutTests/ChangeLog:8
> +        Add a test regerssion test for copying & pasting a line in pre into textarea twice.

Typo regression and I think you don't mean to say test twice.
Comment 10 Ryosuke Niwa 2012-06-27 12:42:27 PDT
Committed r121357: <http://trac.webkit.org/changeset/121357>