Browse Source

Use CKEditorError.

Piotr Jasiun 10 years ago
parent
commit
7190f6f302

+ 57 - 24
packages/ckeditor5-utils/src/document/changeoperation.js

@@ -5,7 +5,7 @@
 
 'use strict';
 
-CKEDITOR.define( [ 'document/operation' ], function( Operation ) {
+CKEDITOR.define( [ 'document/operation', 'ckeditorerror' ], function( Operation, CKEditorError ) {
 	/**
 	 *
 	 *
@@ -28,46 +28,79 @@ CKEDITOR.define( [ 'document/operation' ], function( Operation ) {
 			var oldAttr = this.oldAttr;
 			var newAttr = this.newAttr;
 
-			if ( newAttr === null ) {
-				remove();
-			} else if ( oldAttr === null ) {
-				insert();
-			} else {
-				change();
-			}
+			var value;
 
-			function remove() {
-				for ( var value of range ) {
-					// TODO: if debug
+			// Remove.
+			if ( newAttr === null ) {
+				for ( value of range ) {
 					if ( !value.node.hasAttr( oldAttr ) ) {
-						throw 'The attribute which should be removed does not exists.';
+						/**
+						 * The attribute which should be removed does not exists.
+						 *
+						 * @error operation-change-no-attr-to-remove
+						 * @param {document.ChangeOperation} changeOperation
+						 * @param {document.Node} node
+						 * @param {document.Attribute} attr
+						 */
+						throw new CKEditorError(
+							'operation-change-no-attr-to-remove: The attribute which should be removed does not exists.',
+							{ changeOperation: this, node: value.node, attr: oldAttr } );
 					}
 
 					doRemove( value.node.attrs, oldAttr );
 				}
 			}
-
-			function insert() {
-				for ( var value of range ) {
-					// TODO: if debug
+			// Insert.
+			else if ( oldAttr === null ) {
+				for ( value of range ) {
 					if ( value.node.hasAttr( newAttr.key ) ) {
-						throw 'The attribute with given key already exists.';
+						/**
+						 * The attribute with given key already exists.
+						 *
+						 * @error operation-change-attr-exists
+						 * @param {document.ChangeOperation} changeOperation
+						 * @param {document.Node} node
+						 * @param {document.Attribute} attr
+						 */
+						throw new CKEditorError(
+							'operation-change-attr-exists: The attribute with given key already exists.',
+							{ changeOperation: this, node: value.node, attr: newAttr } );
 					}
 
 					doInsert( value.node.attrs, newAttr );
 				}
 			}
-
-			function change() {
-				for ( var value of range ) {
-					// TODO: if debug
+			// Change.
+			else {
+				for ( value of range ) {
 					if ( oldAttr.key != newAttr.key ) {
-						throw 'Old and new attributes should have the same keys.';
+						/**
+						 * Old and new attributes should have the same keys.
+						 *
+						 * @error operation-change-different-keys
+						 * @param {document.ChangeOperation} changeOperation
+						 * @param {document.Node} node
+						 * @param {document.Attribute} oldAttr
+						 * @param {document.Attribute} newAttr
+						 */
+						throw new CKEditorError(
+							'operation-change-different-keys: Old and new attributes should have the same keys.',
+							{ changeOperation: this, node: value.node, oldAttr: oldAttr, newAttr: newAttr } );
 					}
 
-					// TODO: if debug
 					if ( !value.node.hasAttr( oldAttr ) ) {
-						throw 'The attribute which should be changed does not exists.';
+						/**
+						 * The attribute which should be changed does not exists.
+						 *
+						 * @error operation-change-no-attr-to-change
+						 * @param {document.ChangeOperation} changeOperation
+						 * @param {document.Node} node
+						 * @param {document.Attribute} oldAttr
+						 * @param {document.Attribute} newAttr
+						 */
+						throw new CKEditorError(
+							'operation-change-no-attr-to-change: The attribute which should be changed does not exists.',
+							{ changeOperation: this, node: value.node, oldAttr: oldAttr, newAttr: newAttr } );
 					}
 
 					doRemove( value.node.attrs, oldAttr );

+ 18 - 2
packages/ckeditor5-utils/src/document/document.js

@@ -5,7 +5,12 @@
 
 'use strict';
 
-CKEDITOR.define( [ 'document/element', 'emittermixin', 'utils' ], function( Element, EmitterMixin, utils ) {
+CKEDITOR.define( [
+	'document/element',
+	'emittermixin',
+	'utils',
+	'ckeditorerror'
+], function( Element, EmitterMixin, utils, CKEditorError ) {
 	/**
 	 * Document model.
 	 *
@@ -34,7 +39,18 @@ CKEDITOR.define( [ 'document/element', 'emittermixin', 'utils' ], function( Elem
 		 */
 		applyOperation( operation ) {
 			if ( operation.baseVersion !== this.version ) {
-				throw 'Only operations with matching versions can be applied.';
+				/**
+				 * Only operations with matching versions can be applied.
+				 *
+				 * @error document-applyOperation-wrong-version
+				 * @param {document.Document} doc
+				 * @param {document.Operation} operation
+				 * @param {Number} baseVersion
+				 * @param {Number} documentVersion
+				 */
+				throw new CKEditorError(
+					'document-applyOperation-wrong-version: Only operations with matching versions can be applied.',
+					{ doc: this, operation: operation, baseVersion: operation.baseVersion, documentVersion: this.version } );
 			}
 
 			operation._execute();

+ 10 - 1
packages/ckeditor5-utils/src/document/node.js

@@ -5,7 +5,7 @@
 
 'use strict';
 
-CKEDITOR.define( [ 'document/attribute' ], function( Attribute ) {
+CKEDITOR.define( [ 'document/attribute', 'utils' ], function( Attribute, utils ) {
 	/**
 	 * Abstract document tree node class.
 	 *
@@ -132,6 +132,15 @@ CKEDITOR.define( [ 'document/attribute' ], function( Attribute ) {
 
 			return path;
 		}
+
+		toJSON() {
+			var json = utils.clone( this );
+
+			// Due to circular references we need to remove parent reference.
+			json.parent = this.parent ? this.parent.name : null;
+
+			return json;
+		}
 	}
 
 	return Node;

+ 15 - 3
packages/ckeditor5-utils/src/document/position.js

@@ -5,7 +5,7 @@
 
 'use strict';
 
-CKEDITOR.define( [ 'utils' ], function( utils ) {
+CKEDITOR.define( [ 'utils', 'ckeditorerror' ], function( utils, CKEditorError ) {
 	/**
 	 * Position is always before of after a node.
 	 * See {@link #path} property for more information.
@@ -70,7 +70,13 @@ CKEDITOR.define( [ 'utils' ], function( utils ) {
 		 */
 		static makePositionBefore( node, doc ) {
 			if ( !node.parent ) {
-				throw 'You can not make position before root.';
+				/**
+				 * You can not make position before root.
+				 *
+				 * @error position-before-root
+				 * @param {document.Node} root
+				 */
+				throw new CKEditorError( 'position-before-root: You can not make position before root.', { root: node } );
 			}
 
 			return Position.makePositionFromParentAndOffset( node.parent, node.positionInParent, doc );
@@ -84,7 +90,13 @@ CKEDITOR.define( [ 'utils' ], function( utils ) {
 		 */
 		static makePositionAfter( node, doc ) {
 			if ( !node.parent ) {
-				throw 'You can not make position after root.';
+				/**
+				 * You can not make position after root.
+				 *
+				 * @error position-after-root
+				 * @param {document.Node} root
+				 */
+				throw new CKEditorError( 'position-after-root: You can not make position after root.', { root: node } );
 			}
 
 			return Position.makePositionFromParentAndOffset( node.parent, node.positionInParent + 1, doc );

+ 10 - 5
packages/ckeditor5-utils/tests/document/changeoperation.js

@@ -13,7 +13,8 @@ var modules = bender.amd.require(
 	'document/position',
 	'document/range',
 	'document/character',
-	'document/attribute' );
+	'document/attribute',
+	'ckeditorerror' );
 
 describe( 'ChangeOperation', function() {
 	it( 'should insert attribute to the set of nodes', function() {
@@ -312,6 +313,7 @@ describe( 'ChangeOperation', function() {
 		var Range = modules[ 'document/range' ];
 		var Character = modules[ 'document/character' ];
 		var Attribute = modules[ 'document/attribute' ];
+		var CKEditorError = modules.ckeditorerror;
 
 		var doc = new Document();
 
@@ -325,7 +327,7 @@ describe( 'ChangeOperation', function() {
 				fooAttr,
 				null,
 				doc.version ) );
-		} ).to.throw( 'The attribute which should be removed does not exists.' );
+		} ).to.throw( CKEditorError, /operation-change-no-attr-to-remove/ );
 	} );
 
 	it( 'should throw an error when one try to insert and the attribute already exists', function() {
@@ -335,6 +337,7 @@ describe( 'ChangeOperation', function() {
 		var Range = modules[ 'document/range' ];
 		var Character = modules[ 'document/character' ];
 		var Attribute = modules[ 'document/attribute' ];
+		var CKEditorError = modules.ckeditorerror;
 
 		var doc = new Document();
 
@@ -349,7 +352,7 @@ describe( 'ChangeOperation', function() {
 				null,
 				x2Attr,
 				doc.version ) );
-		} ).to.throw( 'The attribute with given key already exists.' );
+		} ).to.throw( CKEditorError, /operation-change-attr-exists/ );
 	} );
 
 	it( 'should throw an error when one try to change and the new and old attributes have different keys', function() {
@@ -359,6 +362,7 @@ describe( 'ChangeOperation', function() {
 		var Range = modules[ 'document/range' ];
 		var Character = modules[ 'document/character' ];
 		var Attribute = modules[ 'document/attribute' ];
+		var CKEditorError = modules.ckeditorerror;
 
 		var doc = new Document();
 
@@ -373,7 +377,7 @@ describe( 'ChangeOperation', function() {
 				fooAttr,
 				barAttr,
 				doc.version ) );
-		} ).to.throw( 'Old and new attributes should have the same keys.' );
+		} ).to.throw( CKEditorError, /operation-change-different-keys/ );
 	} );
 
 	it( 'should throw an error when one try to change and the old attribute does not exists', function() {
@@ -383,6 +387,7 @@ describe( 'ChangeOperation', function() {
 		var Range = modules[ 'document/range' ];
 		var Character = modules[ 'document/character' ];
 		var Attribute = modules[ 'document/attribute' ];
+		var CKEditorError = modules.ckeditorerror;
 
 		var doc = new Document();
 
@@ -397,6 +402,6 @@ describe( 'ChangeOperation', function() {
 				x1Attr,
 				x2Attr,
 				doc.version ) );
-		} ).to.throw( 'The attribute which should be changed does not exists.' );
+		} ).to.throw( CKEditorError, /operation-change-no-attr-to-change/ );
 	} );
 } );

+ 3 - 2
packages/ckeditor5-utils/tests/document/document.js

@@ -7,7 +7,7 @@
 
 'use strict';
 
-var modules = bender.amd.require( 'document/document', 'document/element' );
+var modules = bender.amd.require( 'document/document', 'document/element', 'ckeditorerror' );
 
 describe( 'constructor', function() {
 	it( 'should create Document with no data', function() {
@@ -43,6 +43,7 @@ describe( 'applyOperation', function() {
 
 	it( 'should throw an error on the operation base version and the document version is different', function() {
 		var Document = modules[ 'document/document' ];
+		var CKEditorError = modules.ckeditorerror;
 
 		var document = new Document();
 		var operationApplied = sinon.spy();
@@ -54,6 +55,6 @@ describe( 'applyOperation', function() {
 
 		expect( function() {
 			document.applyOperation( operation );
-		} ).to.throw();
+		} ).to.throw( CKEditorError, /document-applyOperation-wrong-version/ );
 	} );
 } );

+ 25 - 0
packages/ckeditor5-utils/tests/document/node.js

@@ -181,4 +181,29 @@ describe( 'hasAttr', function() {
 
 		expect( element.hasAttr( fooAttr ) ).to.be.false;
 	} );
+
+	it( 'should create proper JSON string using toJSON method', function() {
+		var Element = modules[ 'document/element' ];
+		var Character = modules[ 'document/character' ];
+
+		var foo = new Element( null, 'foo' );
+		var b = new Character( foo, 'b' );
+		foo.children.push( b );
+
+		var parsedFoo = JSON.parse( JSON.stringify( foo ) );
+		var parsedBar = JSON.parse( JSON.stringify( b ) );
+
+		expect( parsedFoo ).to.be.deep.equals( {
+			name: 'foo',
+			parent: null,
+			attrs: [],
+			children: [ parsedBar ]
+		} );
+
+		expect( parsedBar ).to.be.deep.equals( {
+			character: 'b',
+			parent: 'foo',
+			attrs: []
+		} );
+	} );
 } );

+ 6 - 3
packages/ckeditor5-utils/tests/document/position.js

@@ -11,7 +11,8 @@ var modules = bender.amd.require(
 	'document/element',
 	'document/character',
 	'document/position',
-	'document/document' );
+	'document/document',
+	'ckeditorerror' );
 
 describe( 'position', function() {
 	var Element, Character, Document;
@@ -119,10 +120,11 @@ describe( 'position', function() {
 
 	it( 'should throw error if one try to make positions before root', function() {
 		var Position = modules[ 'document/position' ];
+		var CKEditorError = modules.ckeditorerror;
 
 		expect( function() {
 			Position.makePositionBefore( root, doc );
-		} ).to.throw( 'You can not make position before root.' );
+		} ).to.throw( CKEditorError, /position-before-root/ );
 	} );
 
 	it( 'should make positions after elements', function() {
@@ -147,10 +149,11 @@ describe( 'position', function() {
 
 	it( 'should throw error if one try to make positions after root', function() {
 		var Position = modules[ 'document/position' ];
+		var CKEditorError = modules.ckeditorerror;
 
 		expect( function() {
 			Position.makePositionAfter( root, doc );
-		} ).to.throw( 'You can not make position after root.' );
+		} ).to.throw( CKEditorError, /position-after-root/ );
 	} );
 
 	it( 'should have parent', function() {