Ver Fonte

#396: Remove parent reference in serialized models.

Maciej Gołaszewski há 9 anos atrás
pai
commit
59767522d5

+ 2 - 1
packages/ckeditor5-engine/src/treemodel/delta/delta.js

@@ -140,7 +140,8 @@ export default class Delta {
 
 		json.__className = this.constructor.className;
 
-		json.batch = null;
+		// Remove parent batch to avoid circular dependencies.
+		delete json.batch;
 
 		return json;
 	}

+ 1 - 1
packages/ckeditor5-engine/src/treemodel/node.js

@@ -186,7 +186,7 @@ export default class Node {
 		const json = clone( this );
 
 		// Due to circular references we need to remove parent reference.
-		json.parent = this.parent ? this.parent.name : null;
+		delete json.parent;
 
 		// Serialize attributes as Map object is represented as "{}" when parsing to JSON.
 		json._attrs = [ ...json._attrs ];

+ 1 - 1
packages/ckeditor5-engine/src/treemodel/nodelist.js

@@ -59,7 +59,7 @@ class NodeListText extends Text {
 		const json = clone( this );
 
 		// Due to circular references we need to remove parent reference.
-		json.parent = this.parent ? this.parent.name : null;
+		delete json.parent;
 
 		// Serialize attributes as Map object is represented as "{}" when parsing to JSON.
 		json._attrs = [ ...json._attrs ];

+ 2 - 2
packages/ckeditor5-engine/src/treemodel/operation/operation.js

@@ -85,8 +85,8 @@ export default class Operation {
 
 		json.__className = this.constructor.className;
 
-		// Due to circular references we need to remove parent reference.
-		json.delta = this.delta ? `[${this.delta.constructor.className}]` : null;
+		// Remove parent delta to avoid circular dependencies.
+		delete json.delta;
 
 		return json;
 	}

+ 0 - 9
packages/ckeditor5-engine/tests/treemodel/delta/delta.js

@@ -150,7 +150,6 @@ describe( 'Delta', () => {
 		it( 'should return JSON representation for empty delta', () => {
 			expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
 				__className: FooDelta.className,
-				batch: null,
 				operations: []
 			} );
 		} );
@@ -168,7 +167,6 @@ describe( 'Delta', () => {
 
 			expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
 				__className: FooDelta.className,
-				batch: null,
 				operations: [ treeModelTestUtils.jsonParseStringify( operation ) ]
 			} );
 		} );
@@ -184,7 +182,6 @@ describe( 'Delta', () => {
 
 			expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
 				__className: FooDelta.className,
-				batch: null,
 				operations: [ treeModelTestUtils.jsonParseStringify( operation ) ]
 			} );
 		} );
@@ -201,7 +198,6 @@ describe( 'Delta', () => {
 
 			expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
 				__className: FooDelta.className,
-				batch: null,
 				operations: [ treeModelTestUtils.jsonParseStringify( operation ) ]
 			} );
 		} );
@@ -213,7 +209,6 @@ describe( 'Delta', () => {
 
 			expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
 				__className: FooDelta.className,
-				batch: null,
 				operations: [ treeModelTestUtils.jsonParseStringify( operation ) ]
 			} );
 		} );
@@ -230,7 +225,6 @@ describe( 'Delta', () => {
 
 			expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
 				__className: FooDelta.className,
-				batch: null,
 				operations: [ treeModelTestUtils.jsonParseStringify( operation ) ]
 			} );
 		} );
@@ -246,7 +240,6 @@ describe( 'Delta', () => {
 
 			expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
 				__className: FooDelta.className,
-				batch: null,
 				operations: [ treeModelTestUtils.jsonParseStringify( operation ) ]
 			} );
 		} );
@@ -258,7 +251,6 @@ describe( 'Delta', () => {
 
 			expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
 				__className: FooDelta.className,
-				batch: null,
 				operations: [ treeModelTestUtils.jsonParseStringify( operation ) ]
 			} );
 		} );
@@ -268,7 +260,6 @@ describe( 'Delta', () => {
 
 			expect( treeModelTestUtils.jsonParseStringify( delta ) ).to.deep.equal( {
 				__className: FooDelta.className,
-				batch: null,
 				operations: []
 			} );
 		} );

+ 15 - 24
packages/ckeditor5-engine/tests/treemodel/node.js

@@ -98,15 +98,6 @@ describe( 'Node', () => {
 		} );
 	} );
 
-	it( 'should create proper JSON string using toJSON method', () => {
-		let foo = new Element( 'foo', [], [ 'b' ] );
-
-		let parsedFoo = JSON.parse( JSON.stringify( foo ) );
-
-		expect( parsedFoo.parent ).to.equal( null );
-		expect( parsedFoo._children._nodes[ 0 ].parent ).to.equal( 'foo' );
-	} );
-
 	describe( 'getIndex', () => {
 		it( 'should return null if the parent is null', () => {
 			expect( root.getIndex() ).to.be.null;
@@ -190,8 +181,7 @@ describe( 'Node', () => {
 					_indexMap: [],
 					_nodes: []
 				},
-				name: 'one',
-				parent: null
+				name: 'one'
 			} );
 		} );
 
@@ -204,8 +194,7 @@ describe( 'Node', () => {
 					_indexMap: [],
 					_nodes: []
 				},
-				name: 'one',
-				parent: null
+				name: 'one'
 			} );
 		} );
 
@@ -215,21 +204,23 @@ describe( 'Node', () => {
 				_children: {
 					_indexMap: [ 0, 1, 2 ],
 					_nodes: [
-						{ _attrs: [], _children: { _indexMap: [], _nodes: [] }, name: 'one', parent: null },
+						{ _attrs: [], _children: { _indexMap: [], _nodes: [] }, name: 'one' },
 						{
-							_attrs: [], _children: {
-							_indexMap: [ 0, 0, 1, 2 ], _nodes: [
-								{ _attrs: [], text: 'ba', parent: 'two' },
-								{ _attrs: [], _children: { _indexMap: [], _nodes: [] }, name: 'img', parent: 'two' },
-								{ _attrs: [], text: 'r', parent: 'two' }
-							]
-						}, name: 'two', parent: null
+							_attrs: [],
+							_children: {
+								_indexMap: [ 0, 0, 1, 2 ],
+								_nodes: [
+									{ _attrs: [], text: 'ba' },
+									{ _attrs: [], _children: { _indexMap: [], _nodes: [] }, name: 'img' },
+									{ _attrs: [], text: 'r' }
+								]
+							},
+							name: 'two'
 						},
-						{ _attrs: [], _children: { _indexMap: [], _nodes: [] }, name: 'three', parent: null }
+						{ _attrs: [], _children: { _indexMap: [], _nodes: [] }, name: 'three' }
 					]
 				},
-				name: null,
-				parent: null
+				name: null
 			} );
 		} );
 	} );

+ 5 - 6
packages/ckeditor5-engine/tests/treemodel/nodelist.js

@@ -380,10 +380,9 @@ describe( 'NodeList', () => {
 					_attrs: [],
 					_children: {
 						_indexMap: [ 0, 0, 0 ],
-						_nodes: [ { _attrs: [], text: 'bar', parent: 'p' } ]
+						_nodes: [ { _attrs: [], text: 'bar' } ]
 					},
-					name: 'p',
-					parent: null
+					name: 'p'
 				} ]
 			};
 			let jsonParseStringify = treeModelTestUtils.jsonParseStringify( nodeList );
@@ -397,7 +396,7 @@ describe( 'NodeList', () => {
 
 			expect( treeModelTestUtils.jsonParseStringify( nodeList ) ).to.deep.equal( {
 				_nodes: [
-					{ _attrs: [], text: 'bar', parent: null }
+					{ _attrs: [], text: 'bar' }
 				],
 				_indexMap: [ 0, 0, 0 ]
 			} );
@@ -409,7 +408,7 @@ describe( 'NodeList', () => {
 
 			expect( treeModelTestUtils.jsonParseStringify( nodeList ) ).to.deep.equal( {
 				_nodes: [
-					{ _attrs: [ [ 'bold', true ] ], text: 'bar', parent: null }
+					{ _attrs: [ [ 'bold', true ] ], text: 'bar' }
 				],
 				_indexMap: [ 0, 0, 0 ]
 			} );
@@ -421,7 +420,7 @@ describe( 'NodeList', () => {
 
 			expect( treeModelTestUtils.jsonParseStringify( nodeList ) ).to.deep.equal( {
 				_nodes: [
-					{ _attrs: [ [ 'bold', true ] ], text: 'bar', parent: null }
+					{ _attrs: [ [ 'bold', true ] ], text: 'bar' }
 				],
 				_indexMap: [ 0, 0, 0 ]
 			} );

+ 0 - 1
packages/ckeditor5-engine/tests/treemodel/operation/attributeoperation.js

@@ -384,7 +384,6 @@ describe( 'AttributeOperation', () => {
 			expect( serialized ).to.deep.equal( {
 				__className: 'engine.treeModel.operation.AttributeOperation',
 				baseVersion: 0,
-				delta: null,
 				key: 'key',
 				newValue: 'newValue',
 				oldValue: null,

+ 0 - 1
packages/ckeditor5-engine/tests/treemodel/operation/insertoperation.js

@@ -190,7 +190,6 @@ describe( 'InsertOperation', () => {
 			expect( serialized ).to.deep.equal( {
 				__className: 'engine.treeModel.operation.InsertOperation',
 				baseVersion: 0,
-				delta: null,
 				nodeList: treeModelTestUtils.jsonParseStringify( new NodeList( 'x' ) ),
 				position: treeModelTestUtils.jsonParseStringify( position )
 			} );

+ 0 - 1
packages/ckeditor5-engine/tests/treemodel/operation/moveoperation.js

@@ -270,7 +270,6 @@ describe( 'MoveOperation', () => {
 			expect( serialized ).to.deep.equal( {
 				__className: 'engine.treeModel.operation.MoveOperation',
 				baseVersion: 0,
-				delta: null,
 				howMany: 1,
 				isSticky: false,
 				movedRangeStart: treeModelTestUtils.jsonParseStringify( op.movedRangeStart ),

+ 1 - 2
packages/ckeditor5-engine/tests/treemodel/operation/nooperation.js

@@ -44,8 +44,7 @@ describe( 'NoOperation', () => {
 
 			expect( serialized ).to.deep.equal( {
 				__className: 'engine.treeModel.operation.NoOperation',
-				baseVersion: 0,
-				delta: null
+				baseVersion: 0
 			} );
 		} );
 	} );

+ 3 - 6
packages/ckeditor5-engine/tests/treemodel/operation/operation.js

@@ -25,11 +25,9 @@ describe( 'Operation', () => {
 
 		let opOutsideDelta = new Operation( 0 );
 
-		let parsedIn = JSON.parse( JSON.stringify( opInDelta ) );
-		let parsedOutside = JSON.parse( JSON.stringify( opOutsideDelta ) );
+		let parsedOutside = treeModelTestUtils.jsonParseStringify( opOutsideDelta );
 
-		expect( parsedIn.delta ).to.equal( '[engine.treeModel.delta.Delta]' );
-		expect( parsedOutside.delta ).to.be.null;
+		expect( parsedOutside.delta ).to.be.undefined;
 	} );
 
 	describe( 'toJSON', () => {
@@ -40,8 +38,7 @@ describe( 'Operation', () => {
 
 			expect( serialized ).to.deep.equal( {
 				__className: 'engine.treeModel.operation.Operation',
-				baseVersion: 4,
-				delta: null
+				baseVersion: 4
 			} );
 		} );
 	} );

+ 0 - 1
packages/ckeditor5-engine/tests/treemodel/operation/reinsertoperation.js

@@ -101,7 +101,6 @@ describe( 'ReinsertOperation', () => {
 			expect( serialized ).to.deep.equal( {
 				__className: 'engine.treeModel.operation.ReinsertOperation',
 				baseVersion: 0,
-				delta: null,
 				howMany: 2,
 				isSticky: false,
 				movedRangeStart: treeModelTestUtils.jsonParseStringify( operation.movedRangeStart ),

+ 0 - 1
packages/ckeditor5-engine/tests/treemodel/operation/removeoperation.js

@@ -131,7 +131,6 @@ describe( 'RemoveOperation', () => {
 			expect( serialized ).to.deep.equal( {
 				__className: 'engine.treeModel.operation.RemoveOperation',
 				baseVersion: 0,
-				delta: null,
 				howMany: 2,
 				isSticky: false,
 				movedRangeStart: treeModelTestUtils.jsonParseStringify( op.movedRangeStart ),

+ 0 - 1
packages/ckeditor5-engine/tests/treemodel/operation/rootattributeoperation.js

@@ -241,7 +241,6 @@ describe( 'RootAttributeOperation', () => {
 			expect( serialized ).to.deep.equal( {
 				__className: 'engine.treeModel.operation.RootAttributeOperation',
 				baseVersion: 0,
-				delta: null,
 				key: 'key',
 				newValue: 'newValue',
 				oldValue: null,