Selaa lähdekoodia

Tests: Getting to 100% CC

Szymon Cofalik 9 vuotta sitten
vanhempi
sitoutus
137aab82bf

+ 1 - 5
packages/ckeditor5-engine/src/treemodel/schema.js

@@ -100,11 +100,7 @@ export class SchemaItem {
 	 * @param {Array.<String>|String} [attributes] If set, this path will be used only for entities that have attribute(s) with this key.
 	 */
 	_addPath( member, path, attributes ) {
-		if ( typeof path === 'string' ) {
-			path = path.split( ' ' );
-		} else {
-			path = path.slice();
-		}
+		path = path.slice();
 
 		if ( !isArray( attributes ) ) {
 			attributes = [ attributes ];

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

@@ -365,6 +365,44 @@ describe( 'Batch', () => {
 			} );
 		} );
 	} );
+
+	describe( 'change attribute on root element', () => {
+		describe( 'setAttr', () => {
+			it( 'should create the attribute on root', () => {
+				batch.setAttr( 'b', 2, root );
+				expect( getOperationsCount() ).to.equal( 1 );
+				expect( root.getAttribute( 'b' ) ).to.equal( 2 );
+			} );
+
+			it( 'should change the attribute of root', () => {
+				batch.setAttr( 'a', 2, root );
+				expect( getOperationsCount() ).to.equal( 1 );
+				expect( root.getAttribute( 'a' ) ).to.equal( 2 );
+			} );
+
+			it( 'should do nothing if the attribute value is the same', () => {
+				batch.setAttr( 'a', 1, root );
+				expect( getOperationsCount() ).to.equal( 1 );
+				batch.setAttr( 'a', 1, root );
+				expect( getOperationsCount() ).to.equal( 1 );
+				expect( root.getAttribute( 'a' ) ).to.equal( 1 );
+			} );
+		} );
+
+		describe( 'removeAttr', () => {
+			it( 'should remove the attribute from root', () => {
+				batch.setAttr( 'a', 1, root );
+				batch.removeAttr( 'a', root );
+				expect( getOperationsCount() ).to.equal( 2 );
+				expect( root.getAttribute( 'a' ) ).to.be.undefined;
+			} );
+
+			it( 'should do nothing if the attribute is not set', () => {
+				batch.removeAttr( 'b', root );
+				expect( getOperationsCount() ).to.equal( 0 );
+			} );
+		} );
+	} );
 } );
 
 describe( 'AttributeDelta', () => {

+ 14 - 0
packages/ckeditor5-engine/tests/treemodel/nodelist.js

@@ -118,6 +118,20 @@ describe( 'NodeList', () => {
 			expect( nodeList.get( 0 ) ).to.equal( p1 );
 			expect( nodeList.get( 1 ) ).to.equal( p2 );
 		} );
+
+		it( 'should accept DocumentFragment as one of items in input array', () => {
+			let p1 = new Element( 'p' );
+			let p2 = new Element( 'p' );
+			let p3 = new Element( 'p' );
+			let frag = new DocumentFragment( [ p1, p2 ] );
+
+			let nodeList = new NodeList( [ frag, p3 ] );
+
+			expect( nodeList.length ).to.equal( 3 );
+			expect( nodeList.get( 0 ) ).to.equal( p1 );
+			expect( nodeList.get( 1 ) ).to.equal( p2 );
+			expect( nodeList.get( 2 ) ).to.equal( p3 );
+		} );
 	} );
 
 	describe( 'insert', () => {

+ 32 - 0
packages/ckeditor5-engine/tests/treemodel/range.js

@@ -439,6 +439,14 @@ describe( 'Range', () => {
 			expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
 			expect( transformed[ 0 ].end.path ).to.deep.equal( [ 4, 4 ] );
 		} );
+
+		it( 'should move after inserted nodes if the range is collapsed', () => {
+			const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 2 ] ) );
+			const transformed = range.getTransformedByInsertion( new Position( root, [ 3, 2 ] ), 3 );
+
+			expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 5 ] );
+			expect( transformed[ 0 ].end.path ).to.deep.equal( [ 3, 5 ] );
+		} );
 	} );
 
 	describe( 'getTransformedByMove', () => {
@@ -515,6 +523,30 @@ describe( 'Range', () => {
 			expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
 			expect( transformed[ 0 ].end.path ).to.deep.equal( [ 5, 0 ] );
 		} );
+
+		it( 'should shrink range if source contained range start position', () => {
+			const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
+			const transformed = range.getTransformedByMove( new Position( root, [ 3, 1 ] ), new Position( root, [ 8 ] ), 2 );
+
+			expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 1 ] );
+			expect( transformed[ 0 ].end.path ).to.deep.equal( [ 5, 4 ] );
+		} );
+
+		it( 'should shrink range if source contained range end position', () => {
+			const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 5, 4 ] ) );
+			const transformed = range.getTransformedByMove( new Position( root, [ 5, 3 ] ), new Position( root, [ 8 ] ), 2 );
+
+			expect( transformed[ 0 ].start.path ).to.deep.equal( [ 3, 2 ] );
+			expect( transformed[ 0 ].end.path ).to.deep.equal( [ 5, 3 ] );
+		} );
+
+		it( 'should move range if it was contained in moved range', () => {
+			const range = new Range( new Position( root, [ 3, 2 ] ), new Position( root, [ 3, 7 ] ) );
+			const transformed = range.getTransformedByMove( new Position( root, [ 3 ] ), new Position( root, [ 8 ] ), 2 );
+
+			expect( transformed[ 0 ].start.path ).to.deep.equal( [ 6, 2 ] );
+			expect( transformed[ 0 ].end.path ).to.deep.equal( [ 6, 7 ] );
+		} );
 	} );
 
 	describe( 'getDifference', () => {

+ 34 - 0
packages/ckeditor5-engine/tests/treemodel/schema/schema.js

@@ -317,3 +317,37 @@ describe( 'check', () => {
 		} );
 	} );
 } );
+
+describe( 'normalizeQueryPath', () => {
+	it( 'should normalize string with spaces to an array of strings', () => {
+		expect( Schema._normalizeQueryPath( '$root div strong' ) ).to.deep.equal( [ '$root', 'div', 'strong' ] );
+	} );
+
+	it( 'should normalize model position to an array of strings', () => {
+		let doc = new Document();
+		let root = doc.createRoot( 'root', '$root' );
+
+		root.insertChildren( 0, [
+			new Element( 'div', null, [
+				new Element( 'header' )
+			] )
+		] );
+
+		let position = new Position( root, [ 0, 0, 0 ] );
+
+		expect( Schema._normalizeQueryPath( position ) ).to.deep.equal( [ '$root', 'div', 'header' ] );
+	} );
+
+	it( 'should normalize array with strings and model elements to an array of strings and drop unrecognized parts', () => {
+		let input = [
+			'$root',
+			[ 'div' ],
+			new Element( 'div' ),
+			null,
+			new Element( 'p' ),
+			'strong'
+		];
+
+		expect( Schema._normalizeQueryPath( input ) ).to.deep.equal( [ '$root', 'div', 'p', 'strong' ] );
+	} );
+} );