Browse Source

Increased model CC.

Oskar Wróbel 8 years ago
parent
commit
64cd6634b4

+ 1 - 24
packages/ckeditor5-engine/src/model/document.js

@@ -186,30 +186,6 @@ export default class Document {
 	}
 
 	/**
-	 * Enqueues document changes. Any changes to be done on document (mostly using {@link #batch}
-	 * should be placed in the queued callback. If no other plugin is changing document at the moment, the callback will be
-	 * called immediately. Otherwise it will wait for all previously queued changes to finish happening. This way
-	 * queued callback will not interrupt other callbacks.
-	 *
-	 * When all queued changes are done {@link #event:changesDone} event is fired.
-	 *
-	 * @fires changesDone
-	 * @param {Function} callback Callback to enqueue.
-	 */
-	enqueueChanges( callback ) {
-		this._pendingChanges.push( callback );
-
-		if ( this._pendingChanges.length == 1 ) {
-			while ( this._pendingChanges.length ) {
-				this._pendingChanges[ 0 ]();
-				this._pendingChanges.shift();
-			}
-
-			this.fire( 'changesDone' );
-		}
-	}
-
-	/**
 	 * Returns top-level root by its name.
 	 *
 	 * @param {String} [name='main'] Unique root name.
@@ -334,6 +310,7 @@ export default class Document {
 
 		// Due to circular references we need to remove parent reference.
 		json.selection = '[engine.model.DocumentSelection]';
+		json.model = '[engine.model.Model]';
 
 		return json;
 	}

+ 83 - 79
packages/ckeditor5-engine/tests/model/document/document.js

@@ -41,81 +41,7 @@ describe( 'Document', () => {
 		} );
 	} );
 
-	describe( 'getRootNames()', () => {
-		it( 'should return empty iterator if no roots exist', () => {
-			expect( count( doc.getRootNames() ) ).to.equal( 0 );
-		} );
-
-		it( 'should return an iterator of all roots without the graveyard', () => {
-			doc.createRoot( '$root', 'a' );
-			doc.createRoot( '$root', 'b' );
-
-			expect( Array.from( doc.getRootNames() ) ).to.deep.equal( [ 'a', 'b' ] );
-		} );
-	} );
-
-	describe( 'createRoot()', () => {
-		it( 'should create a new RootElement with default element and root names, add it to roots map and return it', () => {
-			const root = doc.createRoot();
-
-			expect( doc.roots.size ).to.equal( 2 );
-			expect( root ).to.be.instanceof( RootElement );
-			expect( root.maxOffset ).to.equal( 0 );
-			expect( root ).to.have.property( 'name', '$root' );
-			expect( root ).to.have.property( 'rootName', 'main' );
-		} );
-
-		it( 'should create a new RootElement with custom element and root names, add it to roots map and return it', () => {
-			const root = doc.createRoot( 'customElementName', 'customRootName' );
-
-			expect( doc.roots.size ).to.equal( 2 );
-			expect( root ).to.be.instanceof( RootElement );
-			expect( root.maxOffset ).to.equal( 0 );
-			expect( root ).to.have.property( 'name', 'customElementName' );
-			expect( root ).to.have.property( 'rootName', 'customRootName' );
-		} );
-
-		it( 'should throw an error when trying to create a second root with the same name', () => {
-			doc.createRoot( '$root', 'rootName' );
-
-			expect(
-				() => {
-					doc.createRoot( '$root', 'rootName' );
-				}
-			).to.throw( CKEditorError, /model-document-createRoot-name-exists/ );
-		} );
-	} );
-
-	describe( 'getRoot()', () => {
-		it( 'should return a RootElement previously created with given name', () => {
-			const newRoot = doc.createRoot();
-			const getRoot = doc.getRoot();
-
-			expect( getRoot ).to.equal( newRoot );
-		} );
-
-		it( 'should throw an error when trying to get non-existent root', () => {
-			expect(
-				() => {
-					doc.getRoot( 'root' );
-				}
-			).to.throw( CKEditorError, /model-document-getRoot-root-not-exist/ );
-		} );
-	} );
-
-	describe( 'hasRoot()', () => {
-		it( 'should return true when Document has RootElement with given name', () => {
-			doc.createRoot();
-
-			expect( doc.hasRoot( 'main' ) ).to.be.true;
-		} );
-
-		it( 'should return false when Document does not have RootElement with given name', () => {
-			expect( doc.hasRoot( 'noroot' ) ).to.be.false;
-		} );
-	} );
-
-	describe.skip( 'applyOperation()', () => {
+	describe( 'model#applyOperation listener', () => {
 		it( 'should increase document version, execute operation and fire event with proper data ' +
 			'when operation is a document operation', () => {
 			const changeCallback = sinon.spy();
@@ -180,7 +106,9 @@ describe( 'Document', () => {
 
 		it( 'should throw an error on the operation base version and the document version is different', () => {
 			const operation = {
-				baseVersion: 1
+				baseVersion: 1,
+				isDocumentOperation: true,
+				_execute: () => {}
 			};
 
 			expect(
@@ -191,6 +119,80 @@ describe( 'Document', () => {
 		} );
 	} );
 
+	describe( 'getRootNames()', () => {
+		it( 'should return empty iterator if no roots exist', () => {
+			expect( count( doc.getRootNames() ) ).to.equal( 0 );
+		} );
+
+		it( 'should return an iterator of all roots without the graveyard', () => {
+			doc.createRoot( '$root', 'a' );
+			doc.createRoot( '$root', 'b' );
+
+			expect( Array.from( doc.getRootNames() ) ).to.deep.equal( [ 'a', 'b' ] );
+		} );
+	} );
+
+	describe( 'createRoot()', () => {
+		it( 'should create a new RootElement with default element and root names, add it to roots map and return it', () => {
+			const root = doc.createRoot();
+
+			expect( doc.roots.size ).to.equal( 2 );
+			expect( root ).to.be.instanceof( RootElement );
+			expect( root.maxOffset ).to.equal( 0 );
+			expect( root ).to.have.property( 'name', '$root' );
+			expect( root ).to.have.property( 'rootName', 'main' );
+		} );
+
+		it( 'should create a new RootElement with custom element and root names, add it to roots map and return it', () => {
+			const root = doc.createRoot( 'customElementName', 'customRootName' );
+
+			expect( doc.roots.size ).to.equal( 2 );
+			expect( root ).to.be.instanceof( RootElement );
+			expect( root.maxOffset ).to.equal( 0 );
+			expect( root ).to.have.property( 'name', 'customElementName' );
+			expect( root ).to.have.property( 'rootName', 'customRootName' );
+		} );
+
+		it( 'should throw an error when trying to create a second root with the same name', () => {
+			doc.createRoot( '$root', 'rootName' );
+
+			expect(
+				() => {
+					doc.createRoot( '$root', 'rootName' );
+				}
+			).to.throw( CKEditorError, /model-document-createRoot-name-exists/ );
+		} );
+	} );
+
+	describe( 'getRoot()', () => {
+		it( 'should return a RootElement previously created with given name', () => {
+			const newRoot = doc.createRoot();
+			const getRoot = doc.getRoot();
+
+			expect( getRoot ).to.equal( newRoot );
+		} );
+
+		it( 'should throw an error when trying to get non-existent root', () => {
+			expect(
+				() => {
+					doc.getRoot( 'root' );
+				}
+			).to.throw( CKEditorError, /model-document-getRoot-root-not-exist/ );
+		} );
+	} );
+
+	describe( 'hasRoot()', () => {
+		it( 'should return true when Document has RootElement with given name', () => {
+			doc.createRoot();
+
+			expect( doc.hasRoot( 'main' ) ).to.be.true;
+		} );
+
+		it( 'should return false when Document does not have RootElement with given name', () => {
+			expect( doc.hasRoot( 'noroot' ) ).to.be.false;
+		} );
+	} );
+
 	describe( 'selection', () => {
 		it( 'should get updated attributes whenever attribute operation is applied', () => {
 			sinon.spy( doc.selection, '_updateAttributes' );
@@ -523,8 +525,10 @@ describe( 'Document', () => {
 		} );
 	} );
 
-	// @TODO: What for is this test?
-	it.skip( 'should be correctly converted to json', () => {
-		expect( jsonParseStringify( doc ).selection ).to.equal( '[engine.model.DocumentSelection]' );
+	it( 'should be correctly converted to json', () => {
+		const serialized = jsonParseStringify( doc );
+
+		expect( serialized.selection ).to.equal( '[engine.model.DocumentSelection]' );
+		expect( serialized.model ).to.equal( '[engine.model.Model]' );
 	} );
 } );

+ 4 - 5
packages/ckeditor5-engine/tests/model/documentselection.js

@@ -1066,7 +1066,7 @@ describe( 'DocumentSelection', () => {
 			} );
 
 			it( 'are removed only once in case of multi-op deltas', () => {
-				let spy;
+				let batch;
 				const emptyP2 = new Element( 'p', null, 'x' );
 				root.appendChildren( emptyP2 );
 
@@ -1074,15 +1074,14 @@ describe( 'DocumentSelection', () => {
 				emptyP2.setAttribute( fooStoreAttrKey, 'bar' );
 
 				model.change( writer => {
-					spy = sinon.spy( writer, 'removeAttribute' );
-
+					batch = writer.batch;
 					// <emptyP>{}<emptyP2>
 					writer.merge( Position.createAfter( emptyP ) );
 				} );
 
 				expect( emptyP.hasAttribute( fooStoreAttrKey ) ).to.be.false;
-
-				expect( spy.calledOnce ).to.be.true;
+				// Attribute delta is only one.
+				expect( Array.from( batch.deltas, delta => delta.type ) ).to.deep.equal( [ 'merge', 'attribute' ] );
 			} );
 
 			it( 'uses model change to clear attributes', () => {