Browse Source

Remove treeModel.TextNode. Add treeModel.CharacterProxy.

Szymon Cofalik 10 years ago
parent
commit
92a9289054

+ 83 - 0
packages/ckeditor5-engine/src/treemodel/characterproxy.js

@@ -0,0 +1,83 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import Node from './node.js';
+
+/**
+ * A proxy object representing one character stored in the tree data model. It looks and behaves like
+ * normal node, but is a read-only structure. This is a representation of the data. Manipulating it won't affect
+ * the actual nodes in tree model.
+ *
+ * Keep in mind that CharacterProxy is static, meaning that it won't change when tree model changes. For example, if you
+ * have a {treeModel.Element element} `myEl` containing text `foobar` and then assign `let b = myEl.getChild( 3 )` and
+ * then remove all nodes from `myEl`, `b` will still have character `b`, parent `myEl` and offset `3`.
+ *
+ * CharacterProxy is created on the fly basing on tree model. It is not an explicit node in a tree model but
+ * rather represents it. Because of this, it is not advised to store or compare instances of CharacterProxy class.
+ * If you want to keep live reference to a point in a text, you should use {@link treeModel.LivePosition}.
+ *
+ * You should never create an instance of this class by your own. When passing parameters to constructors,
+ * use string literals or {@link treeModel.Text} instead.
+ *
+ * @class treeModel.CharacterProxy
+ */
+export default class CharacterProxy extends Node {
+	/**
+	 * Creates character node proxy.
+	 *
+	 * @param {treeModel.NodeListText} nodeListText Reference to a text object in a node list containing this character.
+	 * @param {Number} index Index of the character in `nodeListText`.
+	 * @protected
+	 * @constructor
+	 */
+	constructor( nodeListText, index ) {
+		super( nodeListText.attrs );
+
+		/**
+		 * Reference to a text object in a node list containing this character.
+		 *
+		 * @protected
+		 * @readonly
+		 * @property {treeModel.NodeListText} nodeListText
+		 */
+		this._nodeListText = nodeListText;
+
+		/**
+		 * Index of the character in `nodeListText`.
+		 *
+		 * @protected
+		 * @readonly
+		 * @property {Number} index
+		 */
+		this._index = index;
+
+		/**
+		 * Character represented by this proxy.
+		 *
+		 * @protected
+		 * @readonly
+		 * @property {String} character
+		 */
+		this.character = nodeListText.text.substr( index, 1 );
+	}
+
+	/**
+	 * @readonly
+	 * @property {treeModel.Element} parent
+	 * @see {@link treeModel.Node#parent}
+	 */
+	get parent() {
+		return this._nodeListText.parent;
+	}
+
+	/**
+	 * Does nothing as parent is readonly property in {@link treeModel.CharacterProxy}.
+	 */
+	set parent( parent ) {
+		/* jshint unused:false */
+	}
+}

+ 0 - 88
packages/ckeditor5-engine/src/treemodel/textnode.js

@@ -1,88 +0,0 @@
-/**
- * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-'use strict';
-
-import Node from './node.js';
-
-/**
- * A proxy object representing one or more characters stored in the tree data model. It looks like and behaves like
- * normal node, but is a readonly structure. This is a representation of the data. Manipulating it won't affect
- * the actual nodes in tree model.
- *
- * TextNode instance is static, meaning that it is up-to-date only for actual tree model state. No mutations
- * of a tree model containing this character are reflected in its properties. Because of this, it is not advised to store
- * instances of TextNode class. Comparing TextNode instances is pointless too. If you want to keep live reference
- * to a point in a text, you should use {@link treeModel.LivePosition}.
- *
- * In most scenarios you should not create an instance of this class by your own. When passing parameters to
- * constructors, use string literals or {@link treeModel.Text} instead.
- *
- * @class treeModel.TextNode
- */
-export default class TextNode extends Node {
-	/**
-	 * Creates text node.
-	 *
-	 * @param {String} text Characters contained in this text node.
-	 * @param {Iterable} attrs Iterable collection of {@link treeModel.Attribute attributes}.
-	 * @constructor
-	 */
-	constructor( textItem, start, length ) {
-		super( textItem.attrs );
-
-		/**
-		 * {@link treeModel.Text} instance that contains text represented by this text node.
-		 *
-		 * @protected
-		 * @readonly
-		 * @property {treeModel.Text} textItem
-		 */
-		this._textItem = textItem;
-
-		/**
-		 * Defines where text represented by this text node starts in original {@link treeModel.Text text} object.
-		 *
-		 * @protected
-		 * @readonly
-		 * @property {Number} start
-		 */
-		this._start = start;
-
-		/**
-		 * Characters contained in this text node.
-		 *
-		 * @protected
-		 * @readonly
-		 * @property {String} text
-		 */
-		this.text = textItem.text.substr( start, length );
-	}
-
-	/**
-	 * @readonly
-	 * @property {treeModel.Element} parent
-	 * @see {@link treeModel.Node#parent}
-	 */
-	get parent() {
-		return this._textItem.parent;
-	}
-
-	/**
-	 * Does nothing as parent is readonly property in {@link treeModel.TextNode}.
-	 */
-	set parent( parent ) {
-		/* jshint unused:false */
-	}
-
-	/**
-	 * @see {@link treeModel.Node#nextSibling}
-	 */
-	get nextSibling() {
-		const index = this.getIndex();
-
-		return ( index !== null && this.parent.getChild( index + this.text.length ) ) || null;
-	}
-}

+ 54 - 0
packages/ckeditor5-engine/tests/treemodel/characterproxy.js

@@ -0,0 +1,54 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: treemodel */
+
+'use strict';
+
+const modules = bender.amd.require(
+	'core/treemodel/node',
+	'core/treemodel/element',
+	'core/treemodel/text',
+	'core/treemodel/attribute'
+);
+
+describe( 'CharacterProxy', () => {
+	let Node, Element, Text, Attribute;
+	let text, element, char;
+
+	before( () => {
+		Node = modules[ 'core/treemodel/node' ];
+		Element = modules[ 'core/treemodel/element' ];
+		Text = modules[ 'core/treemodel/text' ];
+		Attribute = modules[ 'core/treemodel/attribute' ];
+
+		text = new Text( 'abc', [ new Attribute( 'foo', true ) ] );
+		element = new Element( 'div', [], [ new Element( 'p' ), text, new Element( 'p' ) ] );
+	} );
+
+	beforeEach( () => {
+		char = element.getChild( 2 );
+	} );
+
+	it( 'should extend Node class', () => {
+		expect( char ).to.be.instanceof( Node );
+	} );
+
+	it( 'should have correct character property', () => {
+		expect( char ).to.have.property( 'character' ).that.equals( 'b' );
+	} );
+
+	it( 'should have correct parent property', () => {
+		expect( char ).to.have.property( 'parent' ).that.equals( element );
+	} );
+
+	it( 'should have attributes list equal to passed to Text instance', () => {
+		expect( char.attrs.isEqual( text.attrs ) ).to.be.true;
+	} );
+
+	it( 'should return correct index in parent node', () => {
+		expect( char.getIndex() ).to.equal( 2 );
+	} );
+} );

+ 0 - 78
packages/ckeditor5-engine/tests/treemodel/textnode.js

@@ -1,78 +0,0 @@
-/**
- * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-/* bender-tags: treemodel */
-
-'use strict';
-
-const modules = bender.amd.require(
-	'core/treemodel/node',
-	'core/treemodel/element',
-	'core/treemodel/text',
-	'core/treemodel/textnode',
-	'core/treemodel/attribute',
-	'core/treemodel/attributelist'
-);
-
-describe( 'TextNode', () => {
-	let Node, Element, Text, TextNode, Attribute, AttributeList;
-	let text, element, textNode;
-
-	before( () => {
-		Node = modules[ 'core/treemodel/node' ];
-		Element = modules[ 'core/treemodel/element' ];
-		Text = modules[ 'core/treemodel/text' ];
-		TextNode = modules[ 'core/treemodel/textnode' ];
-		Attribute = modules[ 'core/treemodel/attribute' ];
-		AttributeList = modules[ 'core/treemodel/attribute' ];
-
-		text = new Text( 'foobar', [ new Attribute( 'foo', true ) ] );
-		element = new Element( 'div', [], [ new Element( 'p' ), 'abc', text, new Element( 'p' ) ] );
-	} );
-
-	beforeEach( () => {
-		textNode = new TextNode( text, 2, 3 );
-	} );
-
-	it( 'should extend Node class', () => {
-		expect( textNode ).to.be.instanceof( Node );
-	} );
-
-	it( 'should have text property basing on passed Text instance', () => {
-		expect( textNode ).to.have.property( 'text' ).that.equals( 'oba' );
-	} );
-
-	it( 'should have parent property basing on passed Text instance', () => {
-		expect( textNode ).to.have.property( 'parent' ).that.equals( element );
-	} );
-
-	it( 'should have attributes list equal to passed Text instance', () => {
-		expect( textNode.attrs.isEqual( text.attrs ) ).to.be.true;
-	} );
-
-	it( 'should have correct start property', () => {
-		expect( textNode._start ).to.equal( 2 );
-	} );
-
-	it( 'should not change original Text instance when changed', () => {
-		textNode.text = 'ab';
-		textNode._start = 0;
-		textNode.parent = new Element( 'p' );
-		textNode.attrs = new AttributeList();
-
-		expect( text.text ).to.equal( 'foobar' );
-		expect( text.parent ).to.equal( element );
-		expect( text.attrs.size ).to.equal( 1 );
-	} );
-
-	it( 'getIndex should return value with text node offset in original Text instance', () => {
-		expect( textNode.getIndex() ).to.equal( 6 );
-	} );
-
-	it( 'should have nextSibling property which is a node/character placed after the last character from text node', () => {
-		expect( textNode ).to.have.property( 'nextSibling' );
-		expect( textNode.nextSibling.text ).to.equal( 'r' );
-	} );
-} );