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

Set some of TextProxy properties as public and updated docs.

Oskar Wrobel 9 лет назад
Родитель
Сommit
bcb8df0966

+ 2 - 2
packages/ckeditor5-engine/src/view/position.js

@@ -196,7 +196,7 @@ export default class Position {
 	static createAfter( node ) {
 		// {@link engine.view.TextProxy} is not a instance of {@link engine.view.Node} so we need do handle it in specific way.
 		if ( node instanceof TextProxy ) {
-			return new Position( node._textNode, node._index + node._data.length );
+			return new Position( node.textNode, node.index + node.data.length );
 		}
 
 		if ( !node.parent ) {
@@ -221,7 +221,7 @@ export default class Position {
 	static createBefore( node ) {
 		// {@link engine.view.TextProxy} is not a instance of {@link engine.view.Node} so we need do handle it in specific way.
 		if ( node instanceof TextProxy ) {
-			return new Position( node._textNode, node._index );
+			return new Position( node.textNode, node.index );
 		}
 
 		if ( !node.parent ) {

+ 15 - 19
packages/ckeditor5-engine/src/view/textproxy.js

@@ -23,8 +23,8 @@ export default class TextProxy {
 	 * Creates a tree view text proxy.
 	 *
 	 * @param {engine.view.Text} textNode Text node which text proxy is a substring.
-	 * @param {Number} startOffset Offset from beginning of {#_textNodeParent} and first character of {#_data}.
-	 * @param {Number} [length] Length of substring. If is not set then the end offset is at the end of {#_textNodeParent}.
+	 * @param {Number} startOffset Offset from beginning of {#textNode} and first character of {#data}.
+	 * @param {Number} [length] Length of substring. If is not set then the end offset is at the end of {#textNode}.
 	 */
 	constructor( textNode, startOffset, length ) {
 		/**
@@ -38,66 +38,62 @@ export default class TextProxy {
 		/**
 		 * Reference to the {@link engine.view.Text} element which TextProxy is a substring.
 		 *
-		 * @protected
 		 * @readonly
-		 * @member {engine.view.Text} engine.view.TextProxy#_textNode
+		 * @member {engine.view.Text} engine.view.TextProxy#textNode
 		 */
-		this._textNode = textNode;
+		this.textNode = textNode;
 
 		/**
 		 * Index of the substring in the `textParent`.
 		 *
-		 * @protected
 		 * @readonly
-		 * @member {Number} engine.view.TextProxy#_index
+		 * @member {Number} engine.view.TextProxy#index
 		 */
-		this._index = startOffset;
+		this.index = startOffset;
 
 		/**
 		 * The text content.
 		 *
-		 * @protected
 		 * @readonly
-		 * @member {String} engine.view.TextProxy#_data
+		 * @member {String} engine.view.TextProxy#data
 		 */
-
-		this._data = textNode._data.substring(
+		this.data = textNode.data.substring(
 			startOffset,
-			startOffset + ( length || textNode._data.length - startOffset )
+			startOffset + ( length || textNode.data.length - startOffset )
 		);
 	}
 
 	/**
 	 * 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}.
+	 * {#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 text proxy or null.
 	 */
 	getDocument() {
-		return this._textNode.getDocument();
+		return this.textNode.getDocument();
 	}
 
 	/**
-	 * Gets the top parent for the {#_textNode}. If there is no parent {#_textNode} is the root.
+	 * Gets the top parent for the {#textNode}. If there is no parent {#textNode} is the root.
 	 *
 	 * @returns {engine.view.Node}
 	 */
 	getRoot() {
-		return this._textNode.getRoot();
+		return this.textNode.getRoot();
 	}
 
 	/**
 	 * Returns ancestors array of this text proxy.
 	 *
 	 * @param {Object} options Options object.
-	 * @param {Boolean} [options.includeNode=false] When set to `true` {#_textNode} will be also included in parent's 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._textNode : this.parent;
+		let parent = options.includeNode ? this.textNode : this.parent;
 
 		while ( parent !== null ) {
 			ancestors[ options.parentFirst ? 'push' : 'unshift' ]( parent );

+ 19 - 19
packages/ckeditor5-engine/src/view/treewalker.js

@@ -179,9 +179,9 @@ export default class TreeWalker {
 		// Get node just after current position.
 		let node;
 
-		// Text {@link engine.view.Text} element is a specific parent because it contains string instead of child nodes.
+		// Text is a specific parent because it contains string instead of child nodes.
 		if ( parent instanceof Text ) {
-			node = parent._data[ position.offset ];
+			node = parent.data[ position.offset ];
 		} else {
 			node = parent.getChild( position.offset );
 		}
@@ -203,7 +203,7 @@ export default class TreeWalker {
 
 				return this._next();
 			} else {
-				let charactersCount = node._data.length;
+				let charactersCount = node.data.length;
 				let item = node;
 
 				// If text stick out of walker range, we need to cut it and wrap by TextProxy.
@@ -227,7 +227,7 @@ export default class TreeWalker {
 				textLength = 1;
 			} else {
 				// Check if text stick out of walker range.
-				const endOffset = parent === this._boundaryEndParent ? this.boundaries.end.offset : parent._data.length;
+				const endOffset = parent === this._boundaryEndParent ? this.boundaries.end.offset : parent.data.length;
 
 				textLength = endOffset - position.offset;
 			}
@@ -302,12 +302,12 @@ export default class TreeWalker {
 			}
 		} else if ( node instanceof Text ) {
 			if ( this.singleCharacters ) {
-				position = new Position( node, node._data.length );
+				position = new Position( node, node.data.length );
 				this.position = position;
 
 				return this._previous();
 			} else {
-				let charactersCount = node._data.length;
+				let charactersCount = node.data.length;
 				let item = node;
 
 				// If text stick out of walker range, we need to cut it and wrap by TextProxy.
@@ -315,7 +315,7 @@ export default class TreeWalker {
 					const offset = this.boundaries.start.offset;
 
 					item = new TextProxy( node, offset );
-					charactersCount = item._data.length;
+					charactersCount = item.data.length;
 					position = Position.createBefore( item );
 				} else {
 					// If not just keep moving backward.
@@ -342,11 +342,6 @@ export default class TreeWalker {
 
 			const textProxy = new TextProxy( parent, position.offset, textLength );
 
-			// Position at the beginning of Text is always out of Text node, not inside.
-			// if ( position.offset === 0 ) {
-			// 	position = new Position( parent.parent, parent.getIndex() );
-			// }
-
 			this.position = position;
 
 			return this._formatReturnValue( 'TEXT', textProxy, previousPosition, position, textLength );
@@ -372,27 +367,27 @@ export default class TreeWalker {
 	 */
 	_formatReturnValue( type, item, previousPosition, nextPosition, length ) {
 		// Text is a specific parent, because contains string instead of childs.
-		// We decided to not enter to the Text except situations when walker is iterating over every single character,
+		// Walker doesn't enter to the Text except situations when walker is iterating over every single character,
 		// or the bound starts/ends inside the Text. So when the position is at the beginning or at the end of the Text
 		// we move it just before or just after Text.
 		if ( item instanceof TextProxy ) {
 			// Position is at the end of Text.
-			if ( item._index + item._data.length == item._textNode._data.length ) {
+			if ( item.index + item.data.length == item.textNode.data.length ) {
 				if ( this.direction == 'FORWARD' ) {
-					nextPosition = Position.createAfter( item._textNode );
+					nextPosition = Position.createAfter( item.textNode );
 					// When we change nextPosition of returned value we need also update walker current position.
 					this.position = nextPosition;
 				} else {
-					previousPosition = Position.createAfter( item._textNode );
+					previousPosition = Position.createAfter( item.textNode );
 				}
 			}
 
 			// Position is at the begining ot the text.
-			if ( item._index === 0 ) {
+			if ( item.index === 0 ) {
 				if ( this.direction == 'FORWARD' ) {
-					previousPosition = Position.createBefore( item._textNode );
+					previousPosition = Position.createBefore( item.textNode );
 				} else {
-					nextPosition = Position.createBefore( item._textNode );
+					nextPosition = Position.createBefore( item.textNode );
 					// When we change nextPosition of returned value we need also update walker current position.
 					this.position = nextPosition;
 				}
@@ -416,6 +411,7 @@ export default class TreeWalker {
  * Type of the step made by {@link engine.view.TreeWalker}.
  * Possible values: `'ELEMENT_START'` if walker is at the beginning of a node, `'ELEMENT_END'` if walker is at the end
  * of node, or `'TEXT'` if walker traversed over single and multiple characters.
+ * For {@link engine.view.Text} `ELEMENT_START` and `ELEMENT_END` is not returned.
  *
  * @typedef {String} engine.view.TreeWalkerValueType
  */
@@ -432,11 +428,15 @@ export default class TreeWalker {
  * the node using {@link engine.view.Position.createBefore}.
  * * Backward iteration: For `'ELEMENT_START'` it is the first position inside the element. For all other types it is
  * the position after item.
+ * * If the position is at the beginning or at the end of the {@link engine.view.Text} it is always moved from the
+ * inside of the Text to its parent just before or just after Text.
  * @property {engine.view.Position} nextPosition Next position of the iterator.
  * * Forward iteration: For `'ELEMENT_START'` it is the first position inside the element. For all other types it is
  * the position after the item.
  * * Backward iteration: For `'ELEMENT_END'` it is last position inside element. For all other types it is the position
  * before the item.
+ * * If the position is at the beginning or at the end of the {@link engine.view.Text} it is always moved from the
+ * inside of the Text to its parent just before or just after Text.
  * @property {Number} [length] Length of the item. For `'ELEMENT_START'` it is 1. For `'TEXT'` it is
  * the length of the text. For `'ELEMENT_END'` it is undefined.
  */

+ 5 - 5
packages/ckeditor5-engine/tests/view/textproxy.js

@@ -29,16 +29,16 @@ describe( 'TextProxy', () => {
 	describe( 'constructor', () => {
 		it( 'should create TextProxy instance with specified properties', () => {
 			expect( textProxy ).to.have.property( 'parent' ).to.equal( parent );
-			expect( textProxy ).to.have.property( '_data' ).to.equal( 'cde' );
-			expect( textProxy ).to.have.property( '_textNode' ).to.equal( text );
-			expect( textProxy ).to.have.property( '_index' ).to.equal( 2 );
+			expect( textProxy ).to.have.property( 'data' ).to.equal( 'cde' );
+			expect( textProxy ).to.have.property( 'textNode' ).to.equal( text );
+			expect( textProxy ).to.have.property( 'index' ).to.equal( 2 );
 		} );
 
 		it( 'should get text from specified offset to the end of textNode if length is not defined', () => {
 			textProxy = new TextProxy( text, 2 );
 
-			expect( textProxy ).to.have.property( '_data' ).to.equal( 'cdefgh' );
-			expect( textProxy ).to.have.property( '_index' ).to.equal( 2 );
+			expect( textProxy ).to.have.property( 'data' ).to.equal( 'cdefgh' );
+			expect( textProxy ).to.have.property( 'index' ).to.equal( 2 );
 		} );
 	} );
 

+ 2 - 2
packages/ckeditor5-engine/tests/view/treewalker.js

@@ -1002,8 +1002,8 @@ function expectValue( value, expected, options = {} ) {
 }
 
 function expectText( value, expected ) {
-	expect( value.item._data ).to.equal( expected.text );
-	expect( value.length ).to.equal( value.item._data.length );
+	expect( value.item.data ).to.equal( expected.text );
+	expect( value.length ).to.equal( value.item.data.length );
 }
 
 function expectStart( value, expected ) {