8
0
Quellcode durchsuchen

Changed:   chars saved in model are properly preserved when handling mutations.

Szymon Cofalik vor 9 Jahren
Ursprung
Commit
263ca3bab7

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

@@ -178,8 +178,10 @@ class MutationHandler {
 		// It could also be done in mutation observer too, however if any outside plugin would like to
 		// introduce additional events for mutations, they would get already cleaned up version (this may be good or not).
 		const newText = mutation.newText.replace( /\u00A0/g, ' ' );
+		// To have correct `diffResult`, we also compare view node text data with   replaced by space.
+		const oldText = mutation.oldText.replace( /\u00A0/g, ' ' );
 
-		const diffResult = diff( mutation.oldText, newText );
+		const diffResult = diff( oldText, newText );
 		const changes = diffToChanges( diffResult, newText );
 
 		for ( let change of changes ) {

+ 2 - 2
packages/ckeditor5-typing/tests/tickets/49/1.js

@@ -24,10 +24,10 @@ ClassicEditor.create( document.querySelector( '#editor' ), {
 		console.clear();
 
 		const modelData = getModelData( editor.document, { withoutSelection: true } );
-		console.log( 'model:', modelData.replace( /\u00A0/g, '_' ) );
+		console.log( 'model:', modelData.replace( /\u00A0/g, ' ' ) );
 
 		const viewData = getViewData( editor.editing.view, { withoutSelection: true } );
-		console.log( 'view:', viewData.replace( /\u00A0/g, '_' ) );
+		console.log( 'view:', viewData.replace( /\u00A0/g, ' ' ) );
 
 		console.log( 'dom:', editable.innerHTML );
 		console.log( 'editor.getData', editor.getData() );

+ 16 - 5
packages/ckeditor5-typing/tests/tickets/49/1.md

@@ -3,12 +3,23 @@
 
 ### Issue [#49](https://github.com/ckeditor/ckeditor5-typing/issues/49) manual test
 
-There should be no ` ` (`\u00A0`) characters in model and view, but they should be rendered in DOM. Multiple
-consecutive spaces should be allowed, as well as spaces at the beginning or end of element.
+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:
-* All `&nbsp;` from model and view are being changed to `_`.
-* There should be no `_` in view and model 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.
+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.
+
+Test at least this scenario:
+* fire `editor.setData( '<p>&nbsp;x</p>' )`,
+* place caret at the beginning, before space,
+* write `x` -> `&nbsp;` should be preserved in model, view and DOM,
+* place caret at the beginning,
+* press <kbd>space</kbd> -> `&nbsp;` should appear in DOM, but space in model and view,
+* place caret at the beginning,
+* write `x` -> `&nbsp;` should get converted to a space in DOM.