8
0
Просмотр исходного кода

Add restricted document command first tests.

Maciej Gołaszewski 6 лет назад
Родитель
Сommit
176d24b705

+ 1 - 1
packages/ckeditor5-restricted-editing/src/restricteddocument.js

@@ -37,6 +37,6 @@ export default class RestrictedDocument extends Plugin {
 			}
 		} );
 
-		editor.commands.add( 'norRestricted', new RestrictedDocumentCommand( editor ) );
+		editor.commands.add( 'nonRestricted', new RestrictedDocumentCommand( editor ) );
 	}
 }

+ 27 - 1
packages/ckeditor5-restricted-editing/src/restricteddocumentcommand.js

@@ -20,8 +20,34 @@ export default class RestrictedDocumentCommand extends Command {
 		const model = this.editor.model;
 		const doc = model.document;
 
-		this.value = doc.selection.getAttribute( 'nonRestricted' );
+		this.value = !!doc.selection.getAttribute( 'nonRestricted' );
 
 		this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'nonRestricted' );
 	}
+
+	/**
+	 * @inheritDoc
+	 */
+	execute( options = {} ) {
+		const model = this.editor.model;
+		const document = model.document;
+		const selection = document.selection;
+		const valueToSet = ( options.forceValue === undefined ) ? !this.value : options.forceValue;
+
+		model.change( writer => {
+			const ranges = model.schema.getValidRanges( selection.getRanges(), 'nonRestricted' );
+
+			if ( selection.isCollapsed ) {
+				// TODO
+			} else {
+				for ( const range of ranges ) {
+					if ( valueToSet ) {
+						writer.setAttribute( 'nonRestricted', valueToSet, range );
+					} else {
+						writer.removeAttribute( 'nonRestricted', range );
+					}
+				}
+			}
+		} );
+	}
 }

+ 1 - 1
packages/ckeditor5-restricted-editing/tests/restricteddocument.js

@@ -51,7 +51,7 @@ describe( 'RestrictedDocument', () => {
 	} );
 
 	it( 'should register command', () => {
-		const command = editor.commands.get( 'norRestricted' );
+		const command = editor.commands.get( 'nonRestricted' );
 
 		expect( command ).to.be.instanceof( RestrictedDocumentCommand );
 	} );

+ 144 - 0
packages/ckeditor5-restricted-editing/tests/restricteddocumentcommand.js

@@ -0,0 +1,144 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+import RestrictedDocumentCommand from '../src/restricteddocumentcommand';
+
+import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
+
+import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+
+describe( 'RestrictedDocumentCommand', () => {
+	let editor, command, model;
+
+	beforeEach( () => {
+		return ModelTestEditor
+			.create()
+			.then( newEditor => {
+				editor = newEditor;
+				model = editor.model;
+
+				command = new RestrictedDocumentCommand( editor, 'nonRestricted' );
+
+				model.schema.register( 'p', { inheritAllFrom: '$block' } );
+				model.schema.register( 'h1', { inheritAllFrom: '$block' } );
+				model.schema.register( 'img', {
+					allowWhere: [ '$block', '$text' ],
+					isObject: true
+				} );
+
+				editor.model.schema.extend( '$text', { allowAttributes: [ 'nonRestricted' ] } );
+			} );
+	} );
+
+	afterEach( () => {
+		command.destroy();
+
+		return editor.destroy();
+	} );
+
+	describe( 'value', () => {
+		it( 'is true when collapsed selection has the attribute', () => {
+			model.change( writer => {
+				writer.setSelectionAttribute( 'nonRestricted', true );
+			} );
+
+			expect( command.value ).to.be.true;
+		} );
+
+		it( 'is false when collapsed selection does not have the attribute', () => {
+			model.change( writer => {
+				writer.setSelectionAttribute( 'nonRestricted', true );
+			} );
+
+			model.change( writer => {
+				writer.removeSelectionAttribute( 'nonRestricted' );
+			} );
+
+			expect( command.value ).to.be.false;
+		} );
+
+		it( 'is true when selection is inside text with attribute', () => {
+			setData( model, '<p><$text nonRestricted="true">fo[]o</$text></p><h1>bar</h1>' );
+
+			expect( command.value ).to.be.true;
+		} );
+
+		it( 'is true when selection is on text with attribute', () => {
+			setData( model, '<p>foo[<$text nonRestricted="true">bar</$text>]baz</p>' );
+
+			expect( command.value ).to.be.true;
+		} );
+	} );
+
+	describe( 'isEnabled', () => {
+		beforeEach( () => {
+			model.schema.register( 'x', { inheritAllFrom: '$block' } );
+
+			model.schema.addAttributeCheck( ( ctx, attributeName ) => {
+				if ( ctx.endsWith( 'x $text' ) && attributeName == 'nonRestricted' ) {
+					return false;
+				}
+			} );
+		} );
+
+		describe( 'when selection is collapsed', () => {
+			it( 'should return true if attribute is allowed at caret position', () => {
+				setData( model, '<p>f[]oo</p>' );
+				expect( command.isEnabled ).to.be.true;
+			} );
+
+			it( 'should return true if attribute is not allowed at caret position', () => {
+				setData( model, '<x>fo[]o</x>' );
+				expect( command.isEnabled ).to.be.false;
+			} );
+		} );
+
+		describe( 'when selection is not collapsed', () => {
+			it( 'should return true if there is at least one node in selection that can have the attribute', () => {
+				setData( model, '<p>[foo]</p>' );
+				expect( command.isEnabled ).to.be.true;
+			} );
+
+			it( 'should return false if there are no nodes in selection that can have the attribute', () => {
+				setData( model, '<x>[foo]</x>' );
+				expect( command.isEnabled ).to.be.false;
+			} );
+		} );
+	} );
+
+	describe( 'execute()', () => {
+		it( 'should do nothing if the command is disabled', () => {
+			setData( model, '<p>fo[ob]ar</p>' );
+
+			command.isEnabled = false;
+
+			command.execute();
+
+			expect( getData( model ) ).to.equal( '<p>fo[ob]ar</p>' );
+		} );
+
+		it( 'should add attribute on selected nodes if the command value was false', () => {
+			setData( model, '<p>foo[bar]baz</p>' );
+
+			expect( command.value ).to.be.false;
+
+			command.execute();
+
+			expect( command.value ).to.be.true;
+			expect( getData( model ) ).to.equal( '<p>foo[<$text nonRestricted="true">bar</$text>]baz</p>' );
+		} );
+
+		it( 'should remove attribute from selected nodes if the command value was true', () => {
+			setData( model, '<p>abc[<$text nonRestricted="true">foo]bar</$text>xyz</p>' );
+
+			expect( command.value ).to.be.true;
+
+			command.execute();
+
+			expect( getData( model ) ).to.equal( '<p>abc[foo]<$text nonRestricted="true">bar</$text>xyz</p>' );
+			expect( command.value ).to.be.false;
+		} );
+	} );
+} );