[MediaWiki-commits] [Gerrit] Ignore whitespace when generating paste context - change (VisualEditor/VisualEditor)

2014-08-17 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged.

Change subject: Ignore whitespace when generating paste context
..


Ignore whitespace when generating paste context

textStart/End offsets assume there is no inner whitespace so make
sure that is the case.

Bug: 69595
Change-Id: Ibfddaf94aaf7daedf05eec56821994f7f30ddd56
---
M src/ce/ve.ce.Surface.js
M tests/ce/ve.ce.Surface.test.js
2 files changed, 54 insertions(+), 14 deletions(-)

Approvals:
  Catrope: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/src/ce/ve.ce.Surface.js b/src/ce/ve.ce.Surface.js
index b76da37..3ea78a5 100644
--- a/src/ce/ve.ce.Surface.js
+++ b/src/ce/ve.ce.Surface.js
@@ -985,7 +985,7 @@
  * @param {jQuery.Event} e Paste event
  */
 ve.ce.Surface.prototype.beforePaste = function ( e ) {
-   var tx, node, range, rangyRange, sel,
+   var tx, node, range, contextElement, rangyRange, sel,
context, leftText, rightText, textNode, textStart, textEnd,
selection = this.model.getSelection(),
clipboardData = e.originalEvent.clipboardData,
@@ -1020,7 +1020,11 @@
// to the paste target to give CE some context.
textStart = textEnd = 0;
range = node.getRange();
-   context = [ node.getClonedElement() ];
+   contextElement = node.getClonedElement();
+   // Throw away inner whitespace and other internal properties
+   // otherwise our textStart/End offsets may be wrong.
+   delete contextElement.internal;
+   context = [ contextElement ];
// If there is content to the left of the cursor, put a 
placeholder
// character to the left of the cursor
if ( selection.start > range.start ) {
diff --git a/tests/ce/ve.ce.Surface.test.js b/tests/ce/ve.ce.Surface.test.js
index 61e339f..434a758 100644
--- a/tests/ce/ve.ce.Surface.test.js
+++ b/tests/ce/ve.ce.Surface.test.js
@@ -553,7 +553,9 @@
 } );
 
 QUnit.test( 'beforePaste/afterPaste', function ( assert ) {
-   var i, exampleDoc = 'Foo',
+   var i,
+   exampleDoc = 'Foo Baz 
',
+   docLen = 24,
TestEvent = function ( data ) {
this.originalEvent = {
clipboardData: {
@@ -579,7 +581,7 @@
],
remove: []
},
-   { type: 'retain', length: 8 }
+   { type: 'retain', length: 
docLen - 1 }
]
],
msg: 'Text into empty paragraph'
@@ -596,7 +598,7 @@
insert: [ 'B', 'a', 'r' 
],
remove: []
},
-   { type: 'retain', length: 5 }
+   { type: 'retain', length: 
docLen - 4 }
]
],
msg: 'Text into paragraph'
@@ -617,7 +619,7 @@
],
remove: []
},
-   { type: 'retain', length: 5 }
+   { type: 'retain', length: 
docLen - 4 }
]
],
msg: 'Formatted text into paragraph'
@@ -635,7 +637,7 @@
insert: [ 'B', 'a', 'r' 
],
remove: []
},
-   { type: 'retain', length: 5 }
+   { type: 'retain', length: 
docLen - 4 }
]
],
msg: 'Formatted text into paragraph with 
pasteSpecial'
@@ -652,7 +654,7 @@
insert: [ 'B', 'a', 'r' 
],
remove: []
},
-   { type: 'retain', length: 5 }
+   { type: 'retain', length: 
docLen - 4 }
]
],
  

[MediaWiki-commits] [Gerrit] Ignore whitespace when generating paste context - change (VisualEditor/VisualEditor)

2014-08-16 Thread Esanders (Code Review)
Esanders has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/154407

Change subject: Ignore whitespace when generating paste context
..

Ignore whitespace when generating paste context

textStart/End offsets assume there is no inner whitespace so make
sure that is the case.

Bug: 69595
Change-Id: Ibfddaf94aaf7daedf05eec56821994f7f30ddd56
---
M src/ce/ve.ce.Surface.js
M tests/ce/ve.ce.Surface.test.js
2 files changed, 37 insertions(+), 14 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/07/154407/1

diff --git a/src/ce/ve.ce.Surface.js b/src/ce/ve.ce.Surface.js
index b76da37..207840f 100644
--- a/src/ce/ve.ce.Surface.js
+++ b/src/ce/ve.ce.Surface.js
@@ -985,7 +985,7 @@
  * @param {jQuery.Event} e Paste event
  */
 ve.ce.Surface.prototype.beforePaste = function ( e ) {
-   var tx, node, range, rangyRange, sel,
+   var tx, node, range, contextElement, rangyRange, sel,
context, leftText, rightText, textNode, textStart, textEnd,
selection = this.model.getSelection(),
clipboardData = e.originalEvent.clipboardData,
@@ -1020,7 +1020,13 @@
// to the paste target to give CE some context.
textStart = textEnd = 0;
range = node.getRange();
-   context = [ node.getClonedElement() ];
+   contextElement = node.getClonedElement();
+   // Throw away inner whitespace otherwise our textStart/End
+   // offsets will be wrong.
+   if ( contextElement.internal ) {
+   delete contextElement.internal.whitespace;
+   }
+   context = [ contextElement ];
// If there is content to the left of the cursor, put a 
placeholder
// character to the left of the cursor
if ( selection.start > range.start ) {
diff --git a/tests/ce/ve.ce.Surface.test.js b/tests/ce/ve.ce.Surface.test.js
index 61e339f..bf08712 100644
--- a/tests/ce/ve.ce.Surface.test.js
+++ b/tests/ce/ve.ce.Surface.test.js
@@ -553,7 +553,7 @@
 } );
 
 QUnit.test( 'beforePaste/afterPaste', function ( assert ) {
-   var i, exampleDoc = 'Foo',
+   var i, exampleDoc = 'Foo Baz ',
TestEvent = function ( data ) {
this.originalEvent = {
clipboardData: {
@@ -579,7 +579,7 @@
],
remove: []
},
-   { type: 'retain', length: 8 }
+   { type: 'retain', length: 13 }
]
],
msg: 'Text into empty paragraph'
@@ -596,7 +596,7 @@
insert: [ 'B', 'a', 'r' 
],
remove: []
},
-   { type: 'retain', length: 5 }
+   { type: 'retain', length: 10 }
]
],
msg: 'Text into paragraph'
@@ -617,7 +617,7 @@
],
remove: []
},
-   { type: 'retain', length: 5 }
+   { type: 'retain', length: 10 }
]
],
msg: 'Formatted text into paragraph'
@@ -635,7 +635,7 @@
insert: [ 'B', 'a', 'r' 
],
remove: []
},
-   { type: 'retain', length: 5 }
+   { type: 'retain', length: 10 }
]
],
msg: 'Formatted text into paragraph with 
pasteSpecial'
@@ -652,7 +652,7 @@
insert: [ 'B', 'a', 'r' 
],
remove: []
},
-   { type: 'retain', length: 5 }
+   { type: 'retain', length: 10 }
]
],