浏览代码

Merge pull request #72 from ckeditor/t/68

Fix: Replace all ` ` with spaces in text inserted via mutations. Closes #68.
Szymon Cofalik 8 年之前
父节点
当前提交
6f3e7f9b2f

+ 1 - 1
packages/ckeditor5-typing/src/input.js

@@ -218,7 +218,7 @@ class MutationHandler {
 		}
 
 		// Insert appropriate characters basing on `mutation.text`.
-		const insertedText = mutation.newText.substr( firstChangeAt, insertions );
+		const insertedText = newText.substr( firstChangeAt, insertions );
 		this._insert( modelPos, insertedText );
 
 		// If there was `viewSelection` and it got correctly mapped, collapse selection at found model position.

+ 55 - 0
packages/ckeditor5-typing/tests/input.js

@@ -244,6 +244,61 @@ describe( 'Input feature', () => {
 			expect( Batch.prototype.weakInsert.calledOnce ).to.be.true;
 			expect( Batch.prototype.remove.calledOnce ).to.be.true;
 		} );
+
+		it( 'should replace last   with space', () => {
+			model.enqueueChanges( () => {
+				model.selection.setRanges( [
+					ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
+				] );
+			} );
+
+			view.fire( 'mutations', [
+				{
+					type: 'text',
+					oldText: 'foobar',
+					newText: 'foobar\u00A0',
+					node: viewRoot.getChild( 0 ).getChild( 0 )
+				}
+			] );
+
+			expect( getModelData( model ) ).to.equal( '<paragraph>foobar []</paragraph>' );
+			expect( getViewData( view ) ).to.equal( '<p>foobar {}</p>' );
+		} );
+
+		it( 'should replace first &nbsp; with space', () => {
+			model.enqueueChanges( () => {
+				model.selection.setRanges( [
+					ModelRange.createFromParentsAndOffsets( modelRoot.getChild( 0 ), 6, modelRoot.getChild( 0 ), 6 )
+				] );
+			} );
+
+			view.fire( 'mutations', [
+				{
+					type: 'text',
+					oldText: 'foobar',
+					newText: '\u00A0foobar',
+					node: viewRoot.getChild( 0 ).getChild( 0 )
+				}
+			] );
+
+			expect( getModelData( model ) ).to.equal( '<paragraph> foobar[]</paragraph>' );
+			expect( getViewData( view ) ).to.equal( '<p> foobar{}</p>' );
+		} );
+
+		it( 'should replace all &nbsp; with spaces', () => {
+			view.fire( 'mutations', [
+				{
+					type: 'text',
+					oldText: 'foobar',
+					newText: 'foobar\u00A0\u00A0\u00A0baz',
+					node: viewRoot.getChild( 0 ).getChild( 0 )
+				}
+			] );
+
+			expect( getModelData( model ) ).to.equal( '<paragraph>foo[]bar   baz</paragraph>' );
+			expect( getViewData( view ) ).to.equal( '<p>foo{}bar' +
+				'   baz</p>' );
+		} );
 	} );
 
 	describe( 'keystroke handling', () => {

+ 0 - 15
packages/ckeditor5-typing/tests/manual/49/1.md

@@ -1,15 +0,0 @@
-### Issue [#49](https://github.com/ckeditor/ckeditor5-typing/issues/49) manual test
-
-Non-breakable spaces created by browser after using <kbd>space</kbd> should be changed to normal space characters when
-inserted to model and view. Still, `&nbsp;` have to be correctly rendered in DOM.
-
-Multiple consecutive spaces should be allowed, as well as spaces at the beginning or end of element.
-
-Open console and start using <kbd>space</kbd>. After each change, console is refreshed with output:
-* There should be no `&nbsp;` inserted in view and model after using <kbd>space</kbd>.
-* There should be `&nbsp;` in DOM and `getData` output, where needed.
-
-Use `editor.setData()` to test whether whitespaces in input HTML are correctly removed and `&nbsp;` are correctly
-saved in model and view. `&nbsp;` inserted through `editor.setData()` should be kept, not changed to normal spaces.
-
-Use the button to test whether `&nbsp;`s are correctly preserved in model, view and DOM.

packages/ckeditor5-typing/tests/manual/49/1.html → packages/ckeditor5-typing/tests/manual/nbsp.html


+ 1 - 1
packages/ckeditor5-typing/tests/manual/49/1.js

@@ -7,7 +7,7 @@
 
 import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classic';
 import Enter from '@ckeditor/ckeditor5-enter/src/enter';
-import Typing from '../../../src/typing';
+import Typing from '../../src/typing';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';

+ 21 - 0
packages/ckeditor5-typing/tests/manual/nbsp.md

@@ -0,0 +1,21 @@
+### `&nbsp;` handling test
+
+Typing plugin should correctly handle spaces and &nbsp; characters.
+
+**Requirements:**
+
+1. Non-breakable spaces created by browser when using <kbd>space</kbd> should be changed to normal space characters when
+inserted to model (and then, to view).
+2. In DOM we still need to render them as `&nbsp;`s.
+3. Multiple consecutive spaces should be allowed, as well as spaces at the beginning or end of element.
+4. `&nbsp;` characters inserted intentionally should be kept as `&nbsp;` in model (and view).
+5. Whitespaces should be removed when editor data is set.
+
+**Test steps:**
+
+1. Open console and start using <kbd>space</kbd>. After each change, console is refreshed with output:
+    - There should be no `&nbsp;` inserted in view and model after using <kbd>space</kbd>.
+    - There should be `&nbsp;` in DOM and `getData` output, where needed.
+2. Use `editor.setData()` to test whether whitespaces in input HTML are correctly removed and `&nbsp;` are correctly
+saved in model and view. `&nbsp;` inserted through `editor.setData()` should be kept, not changed to normal spaces.
+3. Use the button to test whether `&nbsp;`s are correctly preserved in model, view and DOM.