Преглед на файлове

Added Attribute and AttributeFactory classes with tests.

Piotr Jasiun преди 10 години
родител
ревизия
336431552d

+ 47 - 0
packages/ckeditor5-utils/src/document/attribute.js

@@ -0,0 +1,47 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+CKEDITOR.define( function() {
+	/**
+	 * Linear data attribute. Attributes can store any additional information, its meaning is defined by the code which
+	 * use the.
+	 *
+	 *
+	 * Note that if two attributes has the same meaning the same `Attribute` instance should be used.
+	 * To handle this use {@link document.AttributeFactory}.
+	 *
+	 * @class Attribute
+	 */
+	class Attribute {
+		/**
+		 * Create a new attribute class. Once attribute is created it should not be modified.
+		 *
+		 * @param {String} key Attribute key
+		 * @param {Mixed} value Attribute value
+		 */
+		constructor( key, value ) {
+			this.key = key;
+			this.value = value;
+		}
+
+		/**
+		 * Attribute key
+		 *
+		 * @readonly
+		 * @property {String} key
+		 */
+
+		/**
+		 * Attribute value
+		 *
+		 * @readonly
+		 * @property {Mixed} value
+		 */
+	}
+
+	return Attribute;
+} );

+ 71 - 0
packages/ckeditor5-utils/src/document/attributefactory.js

@@ -0,0 +1,71 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+CKEDITOR.define( [ 'document/attribute', 'utils' ], function( Attribute, utils ) {
+	/**
+	 * Factory to use the same attribute for the same key-values pair, so common attributes use the same object instance.
+	 *
+	 * @class document.AttributeFactory
+	 */
+	class AttributeFactory {
+		/**
+		 * Creates new attribute factory.
+		 */
+		constructor() {
+			this._stored = {};
+		}
+
+		/**
+		 * Create a new attribute or return the one which already exists. Calling `create` with the same key and value
+		 * the same object instance will be used. This methods should be used to get common attributes, like bold.
+		 *
+		 * @param {String} key Attribute key
+		 * @param {Mixed} value Attribute value
+		 * @returns {document.Attribute} New attribute or stored one.
+		 */
+		create( key, value ) {
+			var hash = this._stringify( key, value );
+
+			if ( !this._stored[ hash ] ) {
+				this._stored[ hash ] = new Attribute( key, value );
+			}
+
+			return this._stored[ hash ];
+		}
+
+		/**
+		 * Produce a string hash of an item.
+		 *
+		 * @private
+		 *
+		 * @param {String} key Attribute key
+		 * @param {Mixed} value Attribute value
+		 * @returns {document.Attribute} New attribute or stored one.
+		 */
+		_stringify( key, value ) {
+			return JSON.stringify( key ) + ': ' + JSON.stringify( value, sort );
+
+			// We do no care about the order so collections with the same elements should return the same hash.
+			function sort( key, value ) {
+				if ( !utils.isArray( value ) && utils.isObject( value ) ) {
+					var sorted = {};
+
+					// Sort keys and fill up the sorted object.
+					Object.keys( value ).sort().forEach( function( key ) {
+						sorted[ key ] = value[ key ];
+					} );
+
+					return sorted;
+				} else {
+					return value;
+				}
+			}
+		}
+	}
+
+	return AttributeFactory;
+} );

Файловите разлики са ограничени, защото са твърде много
+ 903 - 801
packages/ckeditor5-utils/src/lib/lodash/lodash-ckeditor.js


+ 9 - 1
packages/ckeditor5-utils/src/utils-lodash.js

@@ -47,7 +47,15 @@
 		 * @member utils
 		 * @method isObject
 		 */
-		'isObject'
+		'isObject',
+
+		/**
+		 * See Lo-Dash: https://lodash.com/docs#isArray
+		 *
+		 * @member utils
+		 * @method isArray
+		 */
+		'isArray'
 	];
 
 	// Make this compatible with CommonJS as well so it can be used in Node (e.g. "grunt lodash").

+ 23 - 0
packages/ckeditor5-utils/tests/document/attribute.js

@@ -0,0 +1,23 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals describe, it, expect, bender */
+
+/* bender-tags: document */
+
+'use strict';
+
+var modules = bender.amd.require( 'document/attribute' );
+
+describe( 'constructor', function() {
+	it( 'should create attribute', function() {
+		var Attribute = modules[ 'document/attribute' ];
+
+		var attr = new Attribute( 'foo', 'bar' );
+
+		expect( attr ).to.have.property( 'key' ).that.equals( 'foo' );
+		expect( attr ).to.have.property( 'value' ).that.equals( 'bar' );
+	} );
+} );

+ 57 - 0
packages/ckeditor5-utils/tests/document/attributefactory.js

@@ -0,0 +1,57 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals describe, it, expect, bender */
+
+/* bender-tags: document */
+
+'use strict';
+
+var modules = bender.amd.require( 'document/attributefactory' );
+
+describe( 'create', function() {
+	it( 'should create the same instance', function() {
+		var AttributeFactory = modules[ 'document/attributefactory' ];
+
+		var factory = new AttributeFactory();
+
+		var attr1 = factory.create( 'foo', 'bar' );
+		var attr2 = factory.create( 'foo', 'bar' );
+
+		expect( attr1 ).to.equals( attr2 );
+		expect( attr1 ).to.have.property( 'key' ).that.equals( 'foo' );
+		expect( attr1 ).to.have.property( 'value' ).that.equals( 'bar' );
+	} );
+
+	it( 'should create the same instance even if object has different order', function() {
+		var AttributeFactory = modules[ 'document/attributefactory' ];
+
+		var factory = new AttributeFactory();
+
+		var attr1 = factory.create( 'foo', { a: 1, b: 2 } );
+		var attr2 = factory.create( 'foo', { b: 2, a: 1 } );
+
+		expect( attr1 ).to.equals( attr2 );
+		expect( attr1 ).to.have.property( 'key' ).that.equals( 'foo' );
+		expect( attr1 ).to.have.property( 'value' ).that.deep.equals( { a: 1, b: 2 } );
+	} );
+
+	it( 'should create different objects', function() {
+		var AttributeFactory = modules[ 'document/attributefactory' ];
+
+		var factory = new AttributeFactory();
+
+		var attr1 = factory.create( 'foo', 'bar' );
+		var attr2 = factory.create( 'foo', 'baz' );
+
+		expect( attr1 ).to.not.equals( attr2 );
+
+		expect( attr1 ).to.have.property( 'key' ).that.equals( 'foo' );
+		expect( attr1 ).to.have.property( 'value' ).that.equals( 'bar' );
+
+		expect( attr1 ).to.have.property( 'key' ).that.equals( 'foo' );
+		expect( attr2 ).to.have.property( 'value' ).that.equals( 'baz' );
+	} );
+} );