Selaa lähdekoodia

Introduced node, element and character classes.

Piotr Jasiun 10 vuotta sitten
vanhempi
commit
f941cb8cc4

+ 35 - 0
packages/ckeditor5-utils/src/document/character.js

@@ -0,0 +1,35 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+CKEDITOR.define( [ 'document/node' ], function( Node ) {
+	/**
+	 * Data structure for character stored in the linear data.
+	 *
+	 * @class Character
+	 */
+	class Character extends Node {
+		/**
+		 * Creates character linear item.
+		 *
+		 * @param {String} character Described character.
+		 */
+		constructor( parent, character, attrs ) {
+			super( parent, attrs );
+
+			this.character = character;
+		}
+
+		/**
+		 * Described character.
+		 *
+		 * @readonly
+		 * @property {String} character
+		 */
+	}
+
+	return Character;
+} );

+ 40 - 0
packages/ckeditor5-utils/src/document/element.js

@@ -0,0 +1,40 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+CKEDITOR.define( [ 'document/node' ], function( Node ) {
+	/**
+	 * Linear data element.
+	 *
+	 * @class document.Element
+	 */
+	class Element extends Node {
+		/**
+		 * Creates linear data element.
+		 *
+		 * This constructor should be used only internally by the document.
+		 *
+		 * @param {document.Element|Null} parent Node parent.
+		 * @param {String} name Node name.
+		 * @param {Array} attrs Array of attributes.
+		 */
+		constructor( parent, name, attrs ) {
+			super( parent, attrs );
+
+			this.name = name;
+			this.children = [];
+		}
+
+		/**
+		 * Element name.
+		 *
+		 * @readonly
+		 * @property {String} name
+		 */
+	}
+
+	return Element;
+} );

+ 96 - 0
packages/ckeditor5-utils/src/document/node.js

@@ -0,0 +1,96 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+CKEDITOR.define( function() {
+	/**
+	 * Abstract document tree node class.
+	 *
+	 * @class document.Node
+	 */
+	class Node {
+		/**
+		 * Creates tree node.
+		 *
+		 * This is an abstract class, it should not be created directly.
+		 *
+		 * @param {document.Element|Null} parent Node parent.
+		 * @param {Array} attrs Array of attributes.
+		 */
+		constructor( parent, attrs ) {
+			this.parent = parent;
+			this.attrs = attrs || [];
+		}
+
+		/**
+		 * Parent element.
+		 *
+		 * @readonly
+		 * @property {document.Element} parent
+		 */
+
+		/**
+		 * Array of attributes.
+		 *
+		 * @property {Array} attr
+		 */
+
+		/**
+		 * Dept of the node, which equals total number of its parents.
+		 *
+		 * @readonly
+		 * @property {Number} depth
+		 */
+		get depth() {
+			var depth = 0;
+			var parent = this.parent;
+
+			while ( parent ) {
+				depth++;
+
+				parent = parent.parent;
+			}
+
+			return depth;
+		}
+
+		/**
+		 * Nodes next sibling or null if it is the last child.
+		 *
+		 * @readonly
+		 * @property {document.Node|Null} nextSibling
+		 */
+		get nextSibling() {
+			var i;
+
+			// No parent or child doesn't exist in parent's children.
+			if ( !this.parent || ( i = this.parent.children.indexOf( this ) ) == -1 ) {
+				return null;
+			}
+
+			return this.parent.children[ i + 1 ] || null;
+		}
+
+		/**
+		 * Nodes previous sibling or null if it is the last child.
+		 *
+		 * @readonly
+		 * @property {document.Node|Null} previousSibling
+		 */
+		get previousSibling() {
+			var i;
+
+			// No parent or child doesn't exist in parent's children.
+			if ( !this.parent || ( i = this.parent.children.indexOf( this ) ) == -1 ) {
+				return null;
+			}
+
+			return this.parent.children[ i - 1 ] || null;
+		}
+	}
+
+	return Node;
+} );

+ 30 - 0
packages/ckeditor5-utils/tests/document/character.js

@@ -0,0 +1,30 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals describe, it, expect, bender */
+
+/* jshint expr: true */
+
+/* bender-tags: document */
+
+'use strict';
+
+var modules = bender.amd.require(
+	'document/character',
+	'document/node' );
+
+describe( 'constructor', function() {
+	it( 'should create Character', function() {
+		var Character = modules[ 'document/character' ];
+		var Node = modules[ 'document/node' ];
+
+		var character = new Character( null, 'f' );
+
+		expect( character ).to.be.an.instanceof( Node );
+
+		expect( character ).to.have.property( 'character' ).that.equals( 'f' );
+		expect( character ).to.have.property( 'attrs' ).that.is.an( 'array' ).and.is.empty;
+	} );
+} );

+ 88 - 0
packages/ckeditor5-utils/tests/document/node.js

@@ -0,0 +1,88 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals describe, it, before, expect, bender */
+
+/* jshint expr: true */
+
+/* bender-tags: document */
+
+'use strict';
+
+var modules = bender.amd.require(
+	'document/element',
+	'document/character' );
+
+describe( 'tree', function() {
+	var Element, Character;
+
+	var root;
+	var one, two, three;
+	var charB, charA, charR, img;
+
+	before( function() {
+		Element = modules[ 'document/element' ];
+		Character = modules[ 'document/character' ];
+
+		root = new Element();
+
+		one = new Element( root );
+		two = new Element( root );
+		three = new Element( root );
+
+		charB = new Character( two, 'b' );
+		charA = new Character( two, 'a' );
+		img = new Element( two, 'img' );
+		charR = new Character( two, 'r' );
+
+		two.children.push( charB );
+		two.children.push( charA );
+		two.children.push( img );
+		two.children.push( charR );
+
+		root.children.push( one );
+		root.children.push( two );
+		root.children.push( three );
+	} );
+
+	it( 'should have proper depth', function() {
+		expect( root ).to.have.property( 'depth' ).that.equals( 0 );
+
+		expect( one ).to.have.property( 'depth' ).that.equals( 1 );
+		expect( two ).to.have.property( 'depth' ).that.equals( 1 );
+		expect( three ).to.have.property( 'depth' ).that.equals( 1 );
+
+		expect( charB ).to.have.property( 'depth' ).that.equals( 2 );
+		expect( charA ).to.have.property( 'depth' ).that.equals( 2 );
+		expect( img ).to.have.property( 'depth' ).that.equals( 2 );
+		expect( charR ).to.have.property( 'depth' ).that.equals( 2 );
+	} );
+
+	it( 'should have proper nextSibling', function() {
+		expect( root ).to.have.property( 'nextSibling' ).that.is.null;
+
+		expect( one ).to.have.property( 'nextSibling' ).that.equals( two );
+		expect( two ).to.have.property( 'nextSibling' ).that.equals( three );
+		expect( three ).to.have.property( 'nextSibling' ).that.is.null;
+
+		expect( charB ).to.have.property( 'nextSibling' ).that.equals( charA );
+		expect( charA ).to.have.property( 'nextSibling' ).that.equals( img );
+		expect( img ).to.have.property( 'nextSibling' ).that.equals( charR );
+		expect( charR ).to.have.property( 'nextSibling' ).that.is.null;
+	} );
+
+	it( 'should have proper previousSibling', function() {
+		expect( root ).to.have.property( 'previousSibling' ).that.is.expect;
+
+		expect( one ).to.have.property( 'previousSibling' ).that.is.null;
+		expect( two ).to.have.property( 'previousSibling' ).that.equals( one );
+		expect( three ).to.have.property( 'previousSibling' ).that.equals( two );
+
+		expect( charB ).to.have.property( 'previousSibling' ).that.is.null;
+		expect( charA ).to.have.property( 'previousSibling' ).that.equals( charB );
+		expect( img ).to.have.property( 'previousSibling' ).that.equals( charA );
+		expect( charR ).to.have.property( 'previousSibling' ).that.equals( img );
+	} );
+} );