8
0
Pārlūkot izejas kodu

Added: engine.treeModel.Range.createOnElement.

Szymon Cofalik 9 gadi atpakaļ
vecāks
revīzija
229bdb9706

+ 10 - 0
packages/ckeditor5-engine/src/treemodel/range.js

@@ -426,6 +426,16 @@ export default class Range {
 		return this.createFromParentsAndOffsets( element, 0, element, element.getChildCount() );
 		return this.createFromParentsAndOffsets( element, 0, element, element.getChildCount() );
 	}
 	}
 
 
+	/**
+	 * Creates a range on given element only. The range starts just before the element and ends before the first child of the element.
+	 *
+	 * @param {engine.treeModel.Element} element Element on which range should be created.
+	 * @returns {engine.treeModel.Range} Created range.
+	 */
+	static createOnElement( element ) {
+		return this.createFromParentsAndOffsets( element.parent, element.getIndex(), element, 0 );
+	}
+
 	/**
 	/**
 	 * Creates a new range spreading from specified position to the same position moved by given shift.
 	 * Creates a new range spreading from specified position to the same position moved by given shift.
 	 *
 	 *

+ 10 - 1
packages/ckeditor5-engine/tests/treemodel/range.js

@@ -137,7 +137,7 @@ describe( 'Range', () => {
 		//     |- f
 		//     |- f
 		//     |- o
 		//     |- o
 		//     |- z
 		//     |- z
-		before( () => {
+		beforeEach( () => {
 			f = new Text( 'f' );
 			f = new Text( 'f' );
 			o = new Text( 'o' );
 			o = new Text( 'o' );
 			z = new Text( 'z' );
 			z = new Text( 'z' );
@@ -156,6 +156,15 @@ describe( 'Range', () => {
 			} );
 			} );
 		} );
 		} );
 
 
+		describe( 'createOnElement', () => {
+			it( 'should return range', () => {
+				const range = Range.createOnElement( p );
+
+				expect( range.start.path ).to.deep.equal( [ 0 ] );
+				expect( range.end.path ).to.deep.equal( [ 0, 0 ] );
+			} );
+		} );
+
 		describe( 'createFromParentsAndOffsets', () => {
 		describe( 'createFromParentsAndOffsets', () => {
 			it( 'should return range', () => {
 			it( 'should return range', () => {
 				const range = Range.createFromParentsAndOffsets( root, 0, p, 2 );
 				const range = Range.createFromParentsAndOffsets( root, 0, p, 2 );