Browse Source

Added base vaersion of TextProxy class.

Oskar Wrobel 9 years ago
parent
commit
53025b4384

+ 59 - 0
packages/ckeditor5-engine/src/view/textproxy.js

@@ -0,0 +1,59 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+/**
+ * Tree view text proxy.
+ * It is a wrapper for substring of {@link engine.view.Text}.
+ *
+ * @memberOf engine.view
+ */
+export default class TextProxy {
+	/**
+	 * Creates a tree view text proxy.
+	 *
+	 * @param {String} text Substring of {#_textNodeParent}.
+	 * @param {engine.view.Node} parent Parent of {#_textNodeParent}.
+	 * @param {engine.view.Text} textNodeParent Text node which text proxy is a substring.
+     * @param {Number} index Offset from beginning of {#_textNodeParent} and first character of {#_data}.
+     */
+	constructor( text, parent, textNodeParent, index ) {
+		/**
+		 * Element that is a parent of this node.
+		 *
+		 * @readonly
+		 * @member {engine.view.Element|engine.view.DocumentFragment|null} engine.view.Node#parent
+		 */
+		this.parent = parent;
+
+		/**
+		 * Reference to the {@link engine.view.Text} element which TextProxy is a substring.
+		 *
+		 * @protected
+		 * @readonly
+		 * @member {engine.view.Text} engine.view.TextProxy#_textNodeParent
+		 */
+		this._textNodeParent = textNodeParent;
+
+		/**
+		 * Index of the substring in the `textParent`.
+		 *
+		 * @protected
+		 * @readonly
+		 * @member {Number} engine.view.TextProxy#_index
+		 */
+		this._index = index;
+
+		/**
+		 * The text content.
+		 *
+		 * @protected
+		 * @readonly
+		 * @member {String} engine.view.TextProxy#_data
+		 */
+		this._data = text;
+	}
+}

+ 30 - 0
packages/ckeditor5-engine/tests/view/textproxy.js

@@ -0,0 +1,30 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: view */
+
+'use strict';
+
+import TextProxy from '/ckeditor5/engine/view/textproxy.js';
+import Text from '/ckeditor5/engine/view/text.js';
+import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
+
+describe( 'TextProxy', () => {
+	describe( 'constructor', () => {
+		it( 'should create TextProxy instance with specified properties', () => {
+			const text = new Text( 'abcdefgh' );
+			const paragraph = new ContainerElement( 'p', [], [ text ] );
+			const textFragment = 'cdef';
+			const index = 2;
+
+			const textProxy = new TextProxy( textFragment, paragraph, text, index );
+
+			expect( textProxy ).to.have.property( 'parent' ).to.equal( paragraph );
+			expect( textProxy ).to.have.property( '_data' ).to.equal( textFragment );
+			expect( textProxy ).to.have.property( '_textNodeParent' ).to.equal( text );
+			expect( textProxy ).to.have.property( '_index' ).to.equal( index );
+		} );
+	} );
+} );