Browse Source

Improved TextProxy class.

Oskar Wrobel 9 years ago
parent
commit
f85b343123

+ 12 - 25
packages/ckeditor5-engine/src/view/textproxy.js

@@ -5,8 +5,6 @@
 
 'use strict';
 
-import Node from './node.js';
-
 /**
  * Tree view text proxy.
  * It is a wrapper for substring of {@link engine.view.Text}.
@@ -23,7 +21,7 @@ export default class TextProxy {
 	 */
 	constructor( textNode, startOffset, length ) {
 		/**
-		 * Element that is a parent of this node.
+		 * Element that is a parent of this text proxy.
 		 *
 		 * @readonly
 		 * @member {engine.view.Element|engine.view.DocumentFragment|null} engine.view.Node#parent
@@ -63,47 +61,36 @@ export default class TextProxy {
 	}
 
 	/**
-	 * Gets {@link engine.view.Document} reference, from the {@link engine.view.Node#getRoot root} or
-	 * returns null if the root has no reference to the {@link engine.view.Document}.
+	 * Gets {@link engine.view.Document} reference, from the {@link engine.view.Node#getRoot root} of
+	 * {#_textNode} or returns null if the root has no reference to the {@link engine.view.Document}.
 	 *
-	 * @returns {engine.view.Document|null} View Document of the node or null.
+	 * @returns {engine.view.Document|null} View Document of the text proxy or null.
 	 */
 	getDocument() {
-		// Parent might be Node, null or DocumentFragment.
-		if ( this.parent instanceof Node ) {
-			return this.parent.getDocument();
-		} else {
-			return null;
-		}
+		return this._textNode.getDocument();
 	}
 
 	/**
-	 * Gets the top parent for the node. If node has no parent it is the root itself.
+	 * Gets the top parent for the {#_textNode}. If there is no parent {#_textNode} is the root.
 	 *
 	 * @returns {engine.view.Node}
 	 */
 	getRoot() {
-		let root = this;
-
-		while ( root.parent ) {
-			root = root.parent;
-		}
-
-		return root;
+		return this._textNode.getRoot();
 	}
 
 	/**
-	 * Returns ancestors array of this node.
+	 * Returns ancestors array of this text proxy.
 	 *
 	 * @param {Object} options Options object.
-	 * @param {Boolean} [options.includeNode=false] When set to `true` this node will be also included in parent's array.
-	 * @param {Boolean} [options.parentFirst=false] When set to `true`, array will be sorted from node's parent to root element,
-	 * otherwise root element will be the first item in the array.
+	 * @param {Boolean} [options.includeNode=false] When set to `true` {#_textNode} will be also included in parent's array.
+	 * @param {Boolean} [options.parentFirst=false] When set to `true`, array will be sorted from text proxy parent to
+	 * root element, otherwise root element will be the first item in the array.
 	 * @returns {Array} Array with ancestors.
 	 */
 	getAncestors( options = { includeNode: false, parentFirst: false } ) {
 		const ancestors = [];
-		let parent = options.includeNode ? this : this.parent;
+		let parent = options.includeNode ? this._textNode : this.parent;
 
 		while ( parent !== null ) {
 			ancestors[ options.parentFirst ? 'push' : 'unshift' ]( parent );

+ 2 - 9
packages/ckeditor5-engine/tests/view/textproxy.js

@@ -47,13 +47,6 @@ describe( 'TextProxy', () => {
 			expect( textProxy.getDocument() ).to.be.null;
 		} );
 
-		it( 'should return null if has no parent', () => {
-			text = new Text( 'abcdefgh' );
-			textProxy = new TextProxy( text, 1 );
-
-			expect( textProxy.getDocument() ).to.be.null;
-		} );
-
 		it( 'should return Document attached to the parent element', () => {
 			const docMock = createDocumentMock();
 			const root = new RootEditableElement( docMock, 'div' );
@@ -105,14 +98,14 @@ describe( 'TextProxy', () => {
 			expect( result ).to.length( 3 );
 			expect( result[ 0 ] ).to.equal( wrapper );
 			expect( result[ 1 ] ).to.equal( parent );
-			expect( result[ 2 ] ).to.equal( textProxy );
+			expect( result[ 2 ] ).to.equal( text );
 		} );
 
 		it( 'should return array of ancestors including node itself `includeNode` starting from parent `parentFirst`', () => {
 			const result = textProxy.getAncestors( { includeNode: true, parentFirst: true } );
 
 			expect( result.length ).to.equal( 3 );
-			expect( result[ 0 ] ).to.equal( textProxy );
+			expect( result[ 0 ] ).to.equal( text );
 			expect( result[ 1 ] ).to.equal( parent );
 			expect( result[ 2 ] ).to.equal( wrapper );
 		} );