Browse Source

Fix editing behavior after a merge.

Maciej Gołaszewski 6 years ago
parent
commit
abecbe0107

+ 9 - 5
packages/ckeditor5-restricted-editing/src/restrictededitingediting.js

@@ -117,11 +117,15 @@ export default class RestrictedEditingEditing extends Plugin {
 		view.document.registerPostFixer( writer => {
 			const modelSelection = model.document.selection;
 
-			for ( const marker of model.markers.getMarkersAtPosition( modelSelection.anchor ) ) {
-				for ( const viewElement of editor.editing.mapper.markerNameToElements( marker.name ) ) {
-					writer.addClass( HIGHLIGHT_CLASS, viewElement );
-					highlightedMarkers.add( viewElement );
-				}
+			const marker = this._getMarker( modelSelection.anchor );
+
+			if ( !marker ) {
+				return;
+			}
+
+			for ( const viewElement of editor.editing.mapper.markerNameToElements( marker.name ) ) {
+				writer.addClass( HIGHLIGHT_CLASS, viewElement );
+				highlightedMarkers.add( viewElement );
 			}
 		} );
 

+ 1 - 186
packages/ckeditor5-restricted-editing/tests/restrictedediting-commands.js

@@ -8,9 +8,7 @@ import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtest
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import Typing from '@ckeditor/ckeditor5-typing/src/typing';
 import Command from '@ckeditor/ckeditor5-core/src/command';
-import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
-import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
-import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
+import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 import RestrictedEditingEditing from './../src/restrictededitingediting';
 import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
@@ -20,189 +18,6 @@ describe( 'RestrictedEditingEditing - commands', () => {
 
 	testUtils.createSinonSandbox();
 
-	describe( 'editing behavior', () => {
-		let model;
-
-		beforeEach( async () => {
-			editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingEditing ] } );
-			model = editor.model;
-		} );
-
-		afterEach( async () => {
-			await editor.destroy();
-		} );
-
-		it( 'should keep markers in the view when editable region is edited', () => {
-			setModelData( model,
-				'<paragraph>foo bar baz</paragraph>' +
-				'<paragraph>xxx y[]yy zzz</paragraph>'
-			);
-
-			const firstParagraph = model.document.getRoot().getChild( 0 );
-			const secondParagraph = model.document.getRoot().getChild( 1 );
-
-			model.change( writer => {
-				writer.addMarker( 'restricted-editing-exception:1', {
-					range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
-					usingOperation: true,
-					affectsData: true
-				} );
-				writer.addMarker( 'restricted-editing-exception:2', {
-					range: writer.createRange(
-						writer.createPositionAt( secondParagraph, 4 ),
-						writer.createPositionAt( secondParagraph, 7 )
-					),
-					usingOperation: true,
-					affectsData: true
-				} );
-			} );
-
-			model.change( writer => {
-				model.insertContent( writer.createText( 'R', model.document.selection.getAttributes() ) );
-			} );
-
-			const expectedView = '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
-				'<p>xxx <span class="ck-restricted-editing-exception">yRyy</span> zzz</p>';
-
-			expect( editor.getData() ).to.equal( expectedView );
-			expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
-		} );
-
-		it( 'should block user typing outside exception markers', () => {
-			setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
-
-			editor.execute( 'input', { text: 'X' } );
-
-			assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
-		} );
-
-		it( 'should not block user typing inside exception marker', () => {
-			setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
-			const firstParagraph = model.document.getRoot().getChild( 0 );
-
-			model.change( writer => {
-				writer.addMarker( 'restricted-editing-exception:1', {
-					range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
-					usingOperation: true,
-					affectsData: true
-				} );
-			} );
-
-			model.change( writer => {
-				writer.setSelection( firstParagraph, 5 );
-			} );
-			editor.execute( 'input', { text: 'X' } );
-
-			assertEqualMarkup( getModelData( model ), '<paragraph>foo bX[]ar baz</paragraph>' );
-		} );
-
-		it( 'should extend maker when typing on the marker boundary (end)', () => {
-			setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
-			const firstParagraph = model.document.getRoot().getChild( 0 );
-
-			model.change( writer => {
-				writer.addMarker( 'restricted-editing-exception:1', {
-					range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
-					usingOperation: true,
-					affectsData: true
-				} );
-			} );
-
-			editor.execute( 'input', { text: 'X' } );
-
-			assertEqualMarkup( getModelData( model ), '<paragraph>foo barX[] baz</paragraph>' );
-			const markerRange = editor.model.markers.get( 'restricted-editing-exception:1' ).getRange();
-			const expectedRange = model.createRange(
-				model.createPositionAt( firstParagraph, 4 ),
-				model.createPositionAt( firstParagraph, 8 )
-			);
-
-			expect( markerRange.isEqual( expectedRange ) ).to.be.true;
-		} );
-
-		it( 'should extend marker when typing on the marker boundary (start)', () => {
-			setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
-			const firstParagraph = model.document.getRoot().getChild( 0 );
-
-			model.change( writer => {
-				writer.addMarker( 'restricted-editing-exception:1', {
-					range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
-					usingOperation: true,
-					affectsData: true
-				} );
-			} );
-
-			editor.execute( 'input', { text: 'X' } );
-
-			assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
-			const markerRange = editor.model.markers.get( 'restricted-editing-exception:1' ).getRange();
-
-			const expectedRange = model.createRange(
-				model.createPositionAt( firstParagraph, 4 ),
-				model.createPositionAt( firstParagraph, 8 )
-			);
-
-			expect( markerRange.isEqual( expectedRange ) ).to.be.true;
-		} );
-
-		it( 'should extend marker when typing on the marker boundary (collapsed marker)', () => {
-			setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
-			const firstParagraph = model.document.getRoot().getChild( 0 );
-
-			model.change( writer => {
-				writer.addMarker( 'restricted-editing-exception:1', {
-					range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ) ),
-					usingOperation: true,
-					affectsData: true
-				} );
-			} );
-
-			model.change( writer => {
-				writer.setSelection( writer.createPositionAt( firstParagraph, 4 ) );
-			} );
-
-			editor.execute( 'input', { text: 'X' } );
-
-			assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
-			const markerRange = editor.model.markers.get( 'restricted-editing-exception:1' ).getRange();
-
-			const expectedRange = model.createRange(
-				model.createPositionAt( firstParagraph, 4 ),
-				model.createPositionAt( firstParagraph, 5 )
-			);
-
-			expect( markerRange.isEqual( expectedRange ) ).to.be.true;
-		} );
-
-		it( 'should not move collapsed marker to $graveyard', () => {
-			setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
-			const firstParagraph = model.document.getRoot().getChild( 0 );
-
-			model.change( writer => {
-				writer.addMarker( 'restricted-editing-exception:1', {
-					range: writer.createRange(
-						writer.createPositionAt( firstParagraph, 4 ),
-						writer.createPositionAt( firstParagraph, 5 )
-					),
-					usingOperation: true,
-					affectsData: true
-				} );
-			} );
-
-			editor.execute( 'delete' );
-
-			assertEqualMarkup( getModelData( model ), '<paragraph>foo []ar baz</paragraph>' );
-			const markerRange = editor.model.markers.get( 'restricted-editing-exception:1' ).getRange();
-
-			const expectedRange = model.createRange(
-				model.createPositionAt( firstParagraph, 4 ),
-				model.createPositionAt( firstParagraph, 4 )
-			);
-
-			expect( markerRange.isEqual( expectedRange ) ).to.be.true;
-		} );
-	} );
-
 	describe( 'commands behavior', () => {
 		let model, firstParagraph;
 

+ 138 - 2
packages/ckeditor5-restricted-editing/tests/restrictededitingediting.js

@@ -10,12 +10,14 @@ import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictest
 
 import RestrictedEditingEditing from './../src/restrictededitingediting';
 import RestrictedEditingNavigationCommand from '../src/restrictededitingnavigationcommand';
-import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
 import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
 import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
+import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
+import Typing from '@ckeditor/ckeditor5-typing/src/typing';
 
 describe( 'RestrictedEditingEditing', () => {
 	let editor, element;
@@ -144,7 +146,7 @@ describe( 'RestrictedEditingEditing', () => {
 		let model;
 
 		beforeEach( async () => {
-			editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditingEditing ] } );
+			editor = await VirtualTestEditor.create( { plugins: [ Paragraph, Typing, RestrictedEditingEditing ] } );
 			model = editor.model;
 		} );
 
@@ -189,6 +191,140 @@ describe( 'RestrictedEditingEditing', () => {
 				'<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
 				'<p>xxx <span class="ck-restricted-editing-exception ck-restricted-editing-exception_selected">yRyy</span> zzz</p>' );
 		} );
+
+		it( 'should block user typing outside exception markers', () => {
+			setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
+
+			editor.execute( 'input', { text: 'X' } );
+
+			assertEqualMarkup( getModelData( model ), '<paragraph>foo []bar baz</paragraph>' );
+		} );
+
+		it( 'should not block user typing inside exception marker', () => {
+			setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
+			const firstParagraph = model.document.getRoot().getChild( 0 );
+
+			model.change( writer => {
+				writer.addMarker( 'restricted-editing-exception:1', {
+					range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
+					usingOperation: true,
+					affectsData: true
+				} );
+			} );
+
+			model.change( writer => {
+				writer.setSelection( firstParagraph, 5 );
+			} );
+			editor.execute( 'input', { text: 'X' } );
+
+			assertEqualMarkup( getModelData( model ), '<paragraph>foo bX[]ar baz</paragraph>' );
+		} );
+
+		it( 'should extend maker when typing on the marker boundary (end)', () => {
+			setModelData( model, '<paragraph>foo bar[] baz</paragraph>' );
+			const firstParagraph = model.document.getRoot().getChild( 0 );
+
+			model.change( writer => {
+				writer.addMarker( 'restricted-editing-exception:1', {
+					range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
+					usingOperation: true,
+					affectsData: true
+				} );
+			} );
+
+			editor.execute( 'input', { text: 'X' } );
+
+			assertEqualMarkup( getModelData( model ), '<paragraph>foo barX[] baz</paragraph>' );
+			const markerRange = editor.model.markers.get( 'restricted-editing-exception:1' ).getRange();
+			const expectedRange = model.createRange(
+				model.createPositionAt( firstParagraph, 4 ),
+				model.createPositionAt( firstParagraph, 8 )
+			);
+
+			expect( markerRange.isEqual( expectedRange ) ).to.be.true;
+		} );
+
+		it( 'should extend marker when typing on the marker boundary (start)', () => {
+			setModelData( model, '<paragraph>foo []bar baz</paragraph>' );
+			const firstParagraph = model.document.getRoot().getChild( 0 );
+
+			model.change( writer => {
+				writer.addMarker( 'restricted-editing-exception:1', {
+					range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
+					usingOperation: true,
+					affectsData: true
+				} );
+			} );
+
+			editor.execute( 'input', { text: 'X' } );
+
+			assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
+			const markerRange = editor.model.markers.get( 'restricted-editing-exception:1' ).getRange();
+
+			const expectedRange = model.createRange(
+				model.createPositionAt( firstParagraph, 4 ),
+				model.createPositionAt( firstParagraph, 8 )
+			);
+
+			expect( markerRange.isEqual( expectedRange ) ).to.be.true;
+		} );
+
+		it( 'should extend marker when typing on the marker boundary (collapsed marker)', () => {
+			setModelData( model, '<paragraph>[]foo bar baz</paragraph>' );
+			const firstParagraph = model.document.getRoot().getChild( 0 );
+
+			model.change( writer => {
+				writer.addMarker( 'restricted-editing-exception:1', {
+					range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ) ),
+					usingOperation: true,
+					affectsData: true
+				} );
+			} );
+
+			model.change( writer => {
+				writer.setSelection( writer.createPositionAt( firstParagraph, 4 ) );
+			} );
+
+			editor.execute( 'input', { text: 'X' } );
+
+			assertEqualMarkup( getModelData( model ), '<paragraph>foo X[]bar baz</paragraph>' );
+			const markerRange = editor.model.markers.get( 'restricted-editing-exception:1' ).getRange();
+
+			const expectedRange = model.createRange(
+				model.createPositionAt( firstParagraph, 4 ),
+				model.createPositionAt( firstParagraph, 5 )
+			);
+
+			expect( markerRange.isEqual( expectedRange ) ).to.be.true;
+		} );
+
+		it( 'should not move collapsed marker to $graveyard', () => {
+			setModelData( model, '<paragraph>foo b[]ar baz</paragraph>' );
+			const firstParagraph = model.document.getRoot().getChild( 0 );
+
+			model.change( writer => {
+				writer.addMarker( 'restricted-editing-exception:1', {
+					range: writer.createRange(
+						writer.createPositionAt( firstParagraph, 4 ),
+						writer.createPositionAt( firstParagraph, 5 )
+					),
+					usingOperation: true,
+					affectsData: true
+				} );
+			} );
+
+			editor.execute( 'delete' );
+
+			assertEqualMarkup( getModelData( model ), '<paragraph>foo []ar baz</paragraph>' );
+			const markerRange = editor.model.markers.get( 'restricted-editing-exception:1' ).getRange();
+
+			const expectedRange = model.createRange(
+				model.createPositionAt( firstParagraph, 4 ),
+				model.createPositionAt( firstParagraph, 4 )
+			);
+
+			expect( markerRange.isEqual( expectedRange ) ).to.be.true;
+		} );
 	} );
 
 	describe( 'exception highlighting', () => {