瀏覽代碼

Docs and tests for Node.setAttr and Node.removeAttr.

Piotr Jasiun 10 年之前
父節點
當前提交
74c383037a
共有 2 個文件被更改,包括 62 次插入0 次删除
  1. 16 0
      packages/ckeditor5-utils/src/document/node.js
  2. 46 0
      packages/ckeditor5-utils/tests/document/node.js

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

@@ -91,6 +91,11 @@ CKEDITOR.define( [ 'document/attribute', 'utils' ], function( Attribute, utils )
 			return null;
 		}
 
+		/**
+		 * Removes attribute from the list of attribute.
+		 *
+		 * @param {String} key Attribute key.
+		 */
 		removeAttr( key ) {
 			var i, len;
 
@@ -103,10 +108,21 @@ CKEDITOR.define( [ 'document/attribute', 'utils' ], function( Attribute, utils )
 			}
 		}
 
+		/**
+		 * Get number of attributes.
+		 *
+		 * @protected
+		 * @returns {Number} Number of attributes.
+		 */
 		getAttrCount() {
 			return this._attrs.length;
 		}
 
+		/**
+		 * Insert a given attribute. If the attribute with the same key already exists it will be removed.
+		 *
+		 * @param {document.Attribute} attr Attribute to insert.
+		 */
 		setAttr( attr ) {
 			this.removeAttr( attr.key );
 

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

@@ -124,6 +124,52 @@ describe( 'getAttr', function() {
 	} );
 } );
 
+describe( 'setAttr', function() {
+	it( 'should insert an attribute', function() {
+		var Element = modules[ 'document/element' ];
+		var Attribute = modules[ 'document/attribute' ];
+
+		var element = new Element( 'elem' );
+		var attr = new Attribute( 'foo', 'bar' );
+		element.setAttr( attr );
+
+		expect( element.getAttrCount() ).to.equals( 1 );
+		expect( element.getAttr( attr.key ) ).to.equals( attr.value );
+	} );
+
+	it( 'should overwrite attribute with the same key', function() {
+		var Element = modules[ 'document/element' ];
+		var Attribute = modules[ 'document/attribute' ];
+
+		var oldAttr = new Attribute( 'foo', 'bar' );
+		var newAttr = new Attribute( 'foo', 'bar' );
+		var element = new Element( 'elem', [ oldAttr ] );
+
+		element.setAttr( newAttr );
+
+		expect( element.getAttrCount() ).to.equals( 1 );
+		expect( element.getAttr( newAttr.key ) ).to.equals( newAttr.value );
+	} );
+} );
+
+describe( 'removeAttr', function() {
+	it( 'should remove an attribute', function() {
+		var Element = modules[ 'document/element' ];
+		var Attribute = modules[ 'document/attribute' ];
+
+		var attrA = new Attribute( 'a', 'A' );
+		var attrB = new Attribute( 'b', 'b' );
+		var attrC = new Attribute( 'c', 'C' );
+		var element = new Element( 'elem', [ attrA, attrB, attrC ] );
+		element.removeAttr( attrB.key );
+
+		expect( element.getAttrCount() ).to.equals( 2 );
+		expect( element.getAttr( attrA.key ) ).to.equals( attrA.value );
+		expect( element.getAttr( attrC.key ) ).to.equals( attrC.value );
+		expect( element.getAttr( attrB.key ) ).to.be.null;
+	} );
+} );
+
 describe( 'hasAttr', function() {
 	it( 'should check attribute by key', function() {
 		var Element = modules[ 'document/element' ];