Sfoglia il codice sorgente

Introduced position.

Piotr Jasiun 10 anni fa
parent
commit
17667b5f07

+ 64 - 0
packages/ckeditor5-engine/src/document/position.js

@@ -0,0 +1,64 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+CKEDITOR.define( function() {
+	/**
+	 * Position is always before of after a node.
+	 * See {@link #position} property for more information.
+	 *
+	 * @class document.Position
+	 */
+	class Position {
+		/**
+		 * Create a position.
+		 *
+		 * @param {document.node} node Node the position is next to.
+		 * @param {Number} position Possible options: Position.BEFORE or Position.AFTER
+		 */
+		constructor( node, position ) {
+			/**
+			 * Position of the node it the tree. For example:
+			 *
+			 * root          Before: []          After: []
+			 *  |- p         Before: [ 0 ]       After: [ 1 ]
+			 *  |- ul        Before: [ 1 ]       After: [ 2 ]
+			 *     |- li     Before: [ 1, 0 ]    After: [ 1, 1 ]
+			 *     |  |- f   Before: [ 1, 0, 0 ] After: [ 1, 0, 1 ]
+			 *     |  |- o   Before: [ 1, 0, 1 ] After: [ 1, 0, 2 ]
+			 *     |  |- o   Before: [ 1, 0, 2 ] After: [ 1, 0, 3 ]
+			 *     |- li     Before: [ 1, 1 ]    After: [ 1, 2 ]
+			 *        |- b   Before: [ 1, 1, 0 ] After: [ 1, 1, 1 ]
+			 *        |- a   Before: [ 1, 1, 1 ] After: [ 1, 1, 2 ]
+			 *        |- r   Before: [ 1, 1, 2 ] After: [ 1, 1, 3 ]
+			 *
+			 * @type {Array}
+			 */
+			this.position = [];
+
+			var parent = node.parent;
+
+			while ( parent && parent.parent ) {
+				this.position.unshift( parent.positionInParent );
+				parent = parent.parent;
+			}
+
+			// Root have position [].
+			if ( node.parent ) {
+				if ( position === Position.BEFORE ) {
+					this.position.push( node.positionInParent );
+				} else {
+					this.position.push( node.positionInParent + 1 );
+				}
+			}
+		}
+	}
+
+	Position.BEFORE = -1;
+	Position.AFTER = 1;
+
+	return Position;
+} );

+ 111 - 0
packages/ckeditor5-engine/tests/document/position.js

@@ -0,0 +1,111 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: document */
+
+'use strict';
+
+var modules = bender.amd.require(
+	'document/element',
+	'document/character',
+	'document/position' );
+
+describe( 'position', function() {
+	var Element, Character;
+
+	var root, p, ul, li1, li2, f, o, z, b, a, r;
+
+	// root
+	//  |- p         Before: [ 0 ]       After: [ 1 ]
+	//  |- ul        Before: [ 1 ]       After: [ 2 ]
+	//     |- li     Before: [ 1, 0 ]    After: [ 1, 1 ]
+	//     |  |- f   Before: [ 1, 0, 0 ] After: [ 1, 0, 1 ]
+	//     |  |- o   Before: [ 1, 0, 1 ] After: [ 1, 0, 2 ]
+	//     |  |- z   Before: [ 1, 0, 2 ] After: [ 1, 0, 3 ]
+	//     |- li     Before: [ 1, 1 ]    After: [ 1, 2 ]
+	//        |- b   Before: [ 1, 1, 0 ] After: [ 1, 1, 1 ]
+	//        |- a   Before: [ 1, 1, 1 ] After: [ 1, 1, 2 ]
+	//        |- r   Before: [ 1, 1, 2 ] After: [ 1, 1, 3 ]
+	before( function() {
+		Element = modules[ 'document/element' ];
+		Character = modules[ 'document/character' ];
+
+		root = new Element();
+
+		p = new Element( root, 'p' );
+
+		ul = new Element( root, 'ul' );
+
+		li1 = new Element( ul, 'li' );
+
+		f = new Character( li1, 'f' );
+		o = new Character( li1, 'o' );
+		z = new Character( li1, 'z' );
+
+		li2 = new Element( ul, 'li' );
+
+		b = new Character( li2, 'b' );
+		a = new Character( li2, 'a' );
+		r = new Character( li2, 'r' );
+
+		root.children.push( p );
+		root.children.push( ul );
+
+		ul.children.push( li1 );
+		ul.children.push( li2 );
+
+		li1.children.push( f );
+		li1.children.push( o );
+		li1.children.push( z );
+
+		li2.children.push( b );
+		li2.children.push( a );
+		li2.children.push( r );
+	} );
+
+	it( 'should create positions before elements', function() {
+		var Position = modules[ 'document/position' ];
+
+		expect( new Position( root, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [] );
+
+		expect( new Position( p, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 0 ] );
+
+		expect( new Position( ul, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1 ] );
+
+		expect( new Position( li1, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 0 ] );
+
+		expect( new Position( f, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 0, 0 ] );
+		expect( new Position( o, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 0, 1 ] );
+		expect( new Position( z, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 0, 2 ] );
+
+		expect( new Position( li2, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 1 ] );
+
+		expect( new Position( b, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 1, 0 ] );
+		expect( new Position( a, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 1, 1 ] );
+		expect( new Position( r, Position.BEFORE ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 1, 2 ] );
+	} );
+
+	it( 'should create positions after elements', function() {
+		var Position = modules[ 'document/position' ];
+
+		expect( new Position( root, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [] );
+
+		expect( new Position( p, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1 ] );
+
+		expect( new Position( ul, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 2 ] );
+
+		expect( new Position( li1, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 1 ] );
+
+		expect( new Position( f, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 0, 1 ] );
+		expect( new Position( o, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 0, 2 ] );
+		expect( new Position( z, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 0, 3 ] );
+
+		expect( new Position( li2, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 2 ] );
+
+		expect( new Position( b, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 1, 1 ] );
+		expect( new Position( a, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 1, 2 ] );
+		expect( new Position( r, Position.AFTER ) ).to.have.property( 'position' ).that.deep.equals( [ 1, 1, 3 ] );
+	} );
+} );