8
0
Просмотр исходного кода

makePositionFromParentAndOffset, makePositionBefore and makePositionAfter do not need root as a parameter. Added node.root getter property.

Piotr Jasiun 10 лет назад
Родитель
Сommit
ffd871bc3f

+ 16 - 0
packages/ckeditor5-utils/src/document/node.js

@@ -98,6 +98,22 @@ CKEDITOR.define( [ 'document/attribute', 'utils' ], function( Attribute, utils )
 		}
 
 		/**
+		 * The top parent for the node. If node has no parent it is its own root.
+		 *
+		 * @readonly
+		 * @property {Number} depth
+		 */
+		get root() {
+			var root = this; // jscs:ignore safeContextKeyword
+
+			while ( root.parent ) {
+				root = root.parent;
+			}
+
+			return root;
+		}
+
+		/**
 		 * Nodes next sibling or null if it is the last child.
 		 *
 		 * @readonly

+ 6 - 9
packages/ckeditor5-utils/src/document/position.js

@@ -52,23 +52,21 @@ CKEDITOR.define( [ 'utils', 'ckeditorerror' ], function( utils, CKEditorError )
 		 *
 		 * @param {document.Element} parent Position parent element.
 		 * @param {Number} offset Position offset.
-		 * @param {document.Document} doc Document which position refers to.
 		 */
-		static makePositionFromParentAndOffset( parent, offset, root ) {
+		static makePositionFromParentAndOffset( parent, offset ) {
 			var path = parent.getPath();
 
 			path.push( offset );
 
-			return new Position( path, root );
+			return new Position( path, parent.root );
 		}
 
 		/**
 		 * Set the position before given node.
 		 *
 		 * @param {document.node} node Node the position should be directly before.
-		 * @param {document.Document} doc Document which position refers to.
 		 */
-		static makePositionBefore( node, root ) {
+		static makePositionBefore( node ) {
 			if ( !node.parent ) {
 				/**
 				 * You can not make position before root.
@@ -79,16 +77,15 @@ CKEDITOR.define( [ 'utils', 'ckeditorerror' ], function( utils, CKEditorError )
 				throw new CKEditorError( 'position-before-root: You can not make position before root.', { root: node } );
 			}
 
-			return Position.makePositionFromParentAndOffset( node.parent, node.positionInParent, root );
+			return Position.makePositionFromParentAndOffset( node.parent, node.positionInParent );
 		}
 
 		/**
 		 * Set the position after given node.
 		 *
 		 * @param {document.node} node Node the position should be directly after.
-		 * @param {document.Document} doc Document which position refers to.
 		 */
-		static makePositionAfter( node, root ) {
+		static makePositionAfter( node ) {
 			if ( !node.parent ) {
 				/**
 				 * You can not make position after root.
@@ -99,7 +96,7 @@ CKEDITOR.define( [ 'utils', 'ckeditorerror' ], function( utils, CKEditorError )
 				throw new CKEditorError( 'position-after-root: You can not make position after root.', { root: node } );
 			}
 
-			return Position.makePositionFromParentAndOffset( node.parent, node.positionInParent + 1, root );
+			return Position.makePositionFromParentAndOffset( node.parent, node.positionInParent + 1 );
 		}
 
 		/**

+ 6 - 6
packages/ckeditor5-utils/src/document/positioniterator.js

@@ -75,15 +75,15 @@ CKEDITOR.define( [
 			var nodeAfter = position.nodeAfter;
 
 			if ( nodeAfter instanceof Element ) {
-				this.position = Position.makePositionFromParentAndOffset( nodeAfter, 0, position.root );
+				this.position = Position.makePositionFromParentAndOffset( nodeAfter, 0 );
 
 				return formatReturnValue( OPENING_TAG, nodeAfter );
 			} else if ( nodeAfter instanceof Character ) {
-				this.position = Position.makePositionFromParentAndOffset( parent, position.offset + 1, position.root );
+				this.position = Position.makePositionFromParentAndOffset( parent, position.offset + 1 );
 
 				return formatReturnValue( CHARACTER, nodeAfter );
 			} else {
-				this.position = Position.makePositionFromParentAndOffset( parent.parent, parent.positionInParent + 1, position.root );
+				this.position = Position.makePositionFromParentAndOffset( parent.parent, parent.positionInParent + 1 );
 
 				return formatReturnValue( CLOSING_TAG, this.position.nodeBefore );
 			}
@@ -115,15 +115,15 @@ CKEDITOR.define( [
 			var nodeBefore = position.nodeBefore;
 
 			if ( nodeBefore instanceof Element ) {
-				this.position = Position.makePositionFromParentAndOffset( nodeBefore, nodeBefore.children.length, position.root );
+				this.position = Position.makePositionFromParentAndOffset( nodeBefore, nodeBefore.children.length );
 
 				return formatReturnValue( CLOSING_TAG, nodeBefore );
 			} else if ( nodeBefore instanceof Character ) {
-				this.position = Position.makePositionFromParentAndOffset( parent, position.offset - 1, position.root );
+				this.position = Position.makePositionFromParentAndOffset( parent, position.offset - 1 );
 
 				return formatReturnValue( CHARACTER, nodeBefore );
 			} else {
-				this.position = Position.makePositionFromParentAndOffset( parent.parent, parent.positionInParent, position.root );
+				this.position = Position.makePositionFromParentAndOffset( parent.parent, parent.positionInParent );
 
 				return formatReturnValue( OPENING_TAG, this.position.nodeAfter );
 			}

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

@@ -62,6 +62,19 @@ describe( 'tree', function() {
 		expect( charR ).to.have.property( 'depth' ).that.equals( 2 );
 	} );
 
+	it( 'should have proper root', function() {
+		expect( root ).to.have.property( 'root' ).that.equals( root );
+
+		expect( one ).to.have.property( 'root' ).that.equals( root );
+		expect( two ).to.have.property( 'root' ).that.equals( root );
+		expect( three ).to.have.property( 'root' ).that.equals( root );
+
+		expect( charB ).to.have.property( 'root' ).that.equals( root );
+		expect( charA ).to.have.property( 'root' ).that.equals( root );
+		expect( img ).to.have.property( 'root' ).that.equals( root );
+		expect( charR ).to.have.property( 'root' ).that.equals( root );
+	} );
+
 	it( 'should have proper nextSibling', function() {
 		expect( root ).to.have.property( 'nextSibling' ).that.is.null;
 

+ 33 - 33
packages/ckeditor5-utils/tests/document/position.js

@@ -72,40 +72,40 @@ describe( 'position', function() {
 	it( 'should make positions form node and offset', function() {
 		var Position = modules[ 'document/position' ];
 
-		expect( Position.makePositionFromParentAndOffset( root, 0, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
-		expect( Position.makePositionFromParentAndOffset( root, 1, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
-		expect( Position.makePositionFromParentAndOffset( root, 2, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
+		expect( Position.makePositionFromParentAndOffset( root, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
+		expect( Position.makePositionFromParentAndOffset( root, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
+		expect( Position.makePositionFromParentAndOffset( root, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
 
-		expect( Position.makePositionFromParentAndOffset( p, 0, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 0, 0 ] );
+		expect( Position.makePositionFromParentAndOffset( p, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0, 0 ] );
 
-		expect( Position.makePositionFromParentAndOffset( ul, 0, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
-		expect( Position.makePositionFromParentAndOffset( ul, 1, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
-		expect( Position.makePositionFromParentAndOffset( ul, 2, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
+		expect( Position.makePositionFromParentAndOffset( ul, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
+		expect( Position.makePositionFromParentAndOffset( ul, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
+		expect( Position.makePositionFromParentAndOffset( ul, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
 
-		expect( Position.makePositionFromParentAndOffset( li1, 0, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
-		expect( Position.makePositionFromParentAndOffset( li1, 1, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
-		expect( Position.makePositionFromParentAndOffset( li1, 2, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
-		expect( Position.makePositionFromParentAndOffset( li1, 3, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
+		expect( Position.makePositionFromParentAndOffset( li1, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
+		expect( Position.makePositionFromParentAndOffset( li1, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
+		expect( Position.makePositionFromParentAndOffset( li1, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
+		expect( Position.makePositionFromParentAndOffset( li1, 3 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
 	} );
 
 	it( 'should make positions before elements', function() {
 		var Position = modules[ 'document/position' ];
 
-		expect( Position.makePositionBefore( p, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
+		expect( Position.makePositionBefore( p ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
 
-		expect( Position.makePositionBefore( ul, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
+		expect( Position.makePositionBefore( ul ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
 
-		expect( Position.makePositionBefore( li1, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
+		expect( Position.makePositionBefore( li1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
 
-		expect( Position.makePositionBefore( f, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
-		expect( Position.makePositionBefore( o, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
-		expect( Position.makePositionBefore( z, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
+		expect( Position.makePositionBefore( f ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
+		expect( Position.makePositionBefore( o ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
+		expect( Position.makePositionBefore( z ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
 
-		expect( Position.makePositionBefore( li2, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
+		expect( Position.makePositionBefore( li2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
 
-		expect( Position.makePositionBefore( b, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 0 ] );
-		expect( Position.makePositionBefore( a, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
-		expect( Position.makePositionBefore( r, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
+		expect( Position.makePositionBefore( b ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 0 ] );
+		expect( Position.makePositionBefore( a ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
+		expect( Position.makePositionBefore( r ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
 	} );
 
 	it( 'should throw error if one try to make positions before root', function() {
@@ -113,28 +113,28 @@ describe( 'position', function() {
 		var CKEditorError = modules.ckeditorerror;
 
 		expect( function() {
-			Position.makePositionBefore( root, doc.root );
+			Position.makePositionBefore( root );
 		} ).to.throw( CKEditorError, /position-before-root/ );
 	} );
 
 	it( 'should make positions after elements', function() {
 		var Position = modules[ 'document/position' ];
 
-		expect( Position.makePositionAfter( p, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
+		expect( Position.makePositionAfter( p ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
 
-		expect( Position.makePositionAfter( ul, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
+		expect( Position.makePositionAfter( ul ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
 
-		expect( Position.makePositionAfter( li1, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
+		expect( Position.makePositionAfter( li1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
 
-		expect( Position.makePositionAfter( f, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
-		expect( Position.makePositionAfter( o, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
-		expect( Position.makePositionAfter( z, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
+		expect( Position.makePositionAfter( f ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
+		expect( Position.makePositionAfter( o ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
+		expect( Position.makePositionAfter( z ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
 
-		expect( Position.makePositionAfter( li2, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
+		expect( Position.makePositionAfter( li2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
 
-		expect( Position.makePositionAfter( b, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
-		expect( Position.makePositionAfter( a, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
-		expect( Position.makePositionAfter( r, doc.root ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 3 ] );
+		expect( Position.makePositionAfter( b ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
+		expect( Position.makePositionAfter( a ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
+		expect( Position.makePositionAfter( r ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 3 ] );
 	} );
 
 	it( 'should throw error if one try to make positions after root', function() {
@@ -142,7 +142,7 @@ describe( 'position', function() {
 		var CKEditorError = modules.ckeditorerror;
 
 		expect( function() {
-			Position.makePositionAfter( root, doc.root );
+			Position.makePositionAfter( root );
 		} ).to.throw( CKEditorError, /position-after-root/ );
 	} );