8
0
Pārlūkot izejas kodu

NoOperation introduced.

Szymon Cofalik 10 gadi atpakaļ
vecāks
revīzija
c7f1fd27f5

+ 46 - 0
packages/ckeditor5-engine/src/document/operation/nooperation.js

@@ -0,0 +1,46 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+CKEDITOR.define( [
+	'document/operation/operation'
+], ( Operation ) => {
+	/**
+	 * Operation that is doing nothing ("empty operation", "do-nothing operation", "noop").
+	 * This is an operation, which {@link #_execute} method does not change tree model.
+	 * It still has defined some parameters for transformations purposes.
+	 *
+	 * In most cases this operation is a result of transforming operations. When transformation returns
+	 * {@link document.operation.NoOperation} it means that changes done by the transformed operation
+	 * has already been applied.
+	 *
+	 * @class document.operation.NoOperation
+	 */
+	class NoOperation extends Operation {
+		_execute() {
+			// Do nothing.
+		}
+
+		clone( baseVersion ) {
+			/* istanbul ignore else */
+			if ( !baseVersion ) {
+				baseVersion = this.baseVersion;
+			}
+
+			return new NoOperation( baseVersion );
+		}
+
+		getReversed() {
+			return new NoOperation( this.baseVersion + 1 );
+		}
+
+		getTransformedBy() {
+			return this.clone();
+		}
+	}
+
+	return NoOperation;
+} );

+ 100 - 0
packages/ckeditor5-engine/tests/document/operation/nooperation.js

@@ -0,0 +1,100 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: document */
+
+'use strict';
+
+const modules = bender.amd.require(
+	'document/document',
+	'document/position',
+	'document/range',
+	'document/attribute',
+	'document/operation/nooperation',
+	'document/operation/insertoperation',
+	'document/operation/changeoperation',
+	'document/operation/moveoperation'
+);
+
+describe( 'NoOperation', () => {
+	let Document, Position, Range, Attribute, NoOperation, InsertOperation, ChangeOperation, MoveOperation;
+
+	before( function() {
+		Document = modules[ 'document/document' ];
+		Position = modules[ 'document/position' ];
+		Range = modules[ 'document/range' ];
+		Attribute = modules[ 'document/attribute' ];
+		NoOperation = modules[ 'document/operation/nooperation' ];
+		InsertOperation = modules[ 'document/operation/insertoperation' ];
+		ChangeOperation = modules[ 'document/operation/changeoperation' ];
+		MoveOperation = modules[ 'document/operation/moveoperation' ];
+	} );
+
+	let noop, doc, root;
+
+	beforeEach( () => {
+		noop = new NoOperation( 0 );
+		doc = new Document();
+		root = doc.createRoot( 'root' );
+	} );
+
+	function expectNoTransformation( noop, transformBy ) {
+		const transOp = noop.getTransformedBy( transformBy );
+
+		expect( transOp ).to.be.instanceof( NoOperation );
+		expect( transOp.baseVersion ).to.equal( 0 );
+	}
+
+	it( 'should not throw an error when applied', () => {
+		expect( () => doc.applyOperation( noop ) ).to.not.throw( Error );
+	} );
+
+	it( 'should create a do-nothing operation as a reverse', () => {
+		const reverse = noop.getReversed();
+
+		expect( reverse ).to.be.an.instanceof( NoOperation );
+		expect( reverse.baseVersion ).to.equal( 1 );
+	} );
+
+	it( 'should create a do-nothing operation having same parameters when cloned', () => {
+		const clone = noop.clone();
+
+		expect( clone ).to.be.an.instanceof( NoOperation );
+		expect( clone.baseVersion ).to.equal( 0 );
+	} );
+
+	it( 'should not change when transformed by InsertOperation', () => {
+		const transformBy = new InsertOperation( new Position( [ 0 ], root ), 'abc', 0 );
+
+		expectNoTransformation( noop, transformBy );
+	} );
+
+	it( 'should not change when transformed by ChangeOperation', () => {
+		const transformBy = new ChangeOperation(
+			new Range( new Position( [ 0 ], root ), new Position( [ 1 ], root ) ),
+			null,
+			new Attribute( 'foo', 'bar' ),
+			0
+		);
+
+		expectNoTransformation( noop, transformBy );
+	} );
+
+	it( 'should not change when transformed by MoveOperation', () => {
+		const transformBy = new MoveOperation(
+			new Position( [ 0 ], root ),
+			new Position( [ 1 ], root ),
+			4,
+			0
+		);
+
+		expectNoTransformation( noop, transformBy );
+	} );
+
+	it( 'should not change when transformed by NoOperation', () => {
+		const transformBy = new NoOperation( 0 );
+		expectNoTransformation( noop, transformBy );
+	} );
+} );