Ver código fonte

Node attributes are private now.

Piotr Jasiun 10 anos atrás
pai
commit
65b2764301

+ 3 - 21
packages/ckeditor5-utils/src/document/changeoperation.js

@@ -88,7 +88,7 @@ CKEDITOR.define( [ 'document/operation', 'ckeditorerror', 'utils' ], function( O
 								{ changeOperation: this, node: value.node, attr: oldAttr } );
 						}
 
-						doRemove( value.node.attrs, oldAttr );
+						value.node.removeAttr( oldAttr.key );
 					}
 				}
 			}
@@ -110,7 +110,7 @@ CKEDITOR.define( [ 'document/operation', 'ckeditorerror', 'utils' ], function( O
 								{ changeOperation: this, node: value.node, attr: newAttr } );
 						}
 
-						doInsert( value.node.attrs, newAttr );
+						value.node.setAttr( newAttr );
 					}
 				}
 			}
@@ -147,28 +147,10 @@ CKEDITOR.define( [ 'document/operation', 'ckeditorerror', 'utils' ], function( O
 								{ changeOperation: this, node: value.node, oldAttr: oldAttr, newAttr: newAttr } );
 						}
 
-						doRemove( value.node.attrs, oldAttr );
-
-						doInsert( value.node.attrs, newAttr );
+						value.node.setAttr( newAttr );
 					}
 				}
 			}
-
-			function doRemove( attrs, attrToRemove ) {
-				var i, len;
-
-				for ( i = 0, len = attrs.length; i < len; i++ ) {
-					if ( attrs[ i ].isEqual( attrToRemove ) ) {
-						attrs.splice( i, 1 );
-
-						return;
-					}
-				}
-			}
-
-			function doInsert( attrs, attrToInsert ) {
-				attrs.push( attrToInsert );
-			}
 		}
 
 		/**

+ 48 - 14
packages/ckeditor5-utils/src/document/node.js

@@ -35,9 +35,10 @@ CKEDITOR.define( [ 'document/attribute', 'utils' ], function( Attribute, utils )
 			 *
 			 * Attributes of nodes attached to the document can be changed only be the {@link document.ChangeOpeation}.
 			 *
+			 * @private
 			 * @property {Array} attr
 			 */
-			this.attrs = attrs || [];
+			this._attrs = attrs || [];
 		}
 
 		/**
@@ -48,7 +49,26 @@ CKEDITOR.define( [ 'document/attribute', 'utils' ], function( Attribute, utils )
 		 * @returns {Boolean} True if node contains given attribute or attribute with given key.
 		 */
 		hasAttr( attr ) {
-			return this.getAttr( attr ) !== null;
+			var i, len;
+
+			// Attribute
+			if ( attr instanceof Attribute ) {
+				for ( i = 0, len = this._attrs.length; i < len; i++ ) {
+					if ( this._attrs[ i ].isEqual( attr ) ) {
+						return true;
+					}
+				}
+			}
+			// Key
+			else {
+				for ( i = 0, len = this._attrs.length; i < len; i++ ) {
+					if ( this._attrs[ i ].key == attr ) {
+						return true;
+					}
+				}
+			}
+
+			return false;
 		}
 
 		/**
@@ -59,26 +79,40 @@ CKEDITOR.define( [ 'document/attribute', 'utils' ], function( Attribute, utils )
 		 * @returns {document.Attribute} Attribute if node contains given attribute or attribute with given key,
 		 * or null if attribute was not found.
 		 */
-		getAttr( attr ) {
+		getAttr( key ) {
 			var i, len;
 
-			if ( attr instanceof Attribute ) {
-				for ( i = 0, len = this.attrs.length; i < len; i++ ) {
-					if ( this.attrs[ i ].isEqual( attr ) ) {
-						return this.attrs[ i ];
-					}
-				}
-			} else {
-				for ( i = 0, len = this.attrs.length; i < len; i++ ) {
-					if ( this.attrs[ i ].key == attr ) {
-						return this.attrs[ i ];
-					}
+			for ( i = 0, len = this._attrs.length; i < len; i++ ) {
+				if ( this._attrs[ i ].key == key ) {
+					return this._attrs[ i ].value;
 				}
 			}
 
 			return null;
 		}
 
+		removeAttr( key ) {
+			var i, len;
+
+			for ( i = 0, len = this._attrs.length; i < len; i++ ) {
+				if ( this._attrs[ i ].key == key ) {
+					this._attrs.splice( i, 1 );
+
+					return;
+				}
+			}
+		}
+
+		getAttrCount() {
+			return this._attrs.length;
+		}
+
+		setAttr( attr ) {
+			this.removeAttr( attr.key );
+
+			this._attrs.push( attr );
+		}
+
 		/**
 		 * Position of the node in the parent element.
 		 *

+ 3 - 3
packages/ckeditor5-utils/src/document/nodelist.js

@@ -41,9 +41,9 @@ CKEDITOR.define( [
 		 *
 		 *		var nodeList = new NodeList( new Text( 'foo', [ attrA, attrB ] ) );
 		 *		nodeList.length // 3
-		 *		nodeList.get( 0 ).attrs.length // 2
-		 *		nodeList.get( 1 ).attrs.length // 2
-		 *		nodeList.get( 2 ).attrs.length // 2
+		 *		nodeList.get( 0 ).getAttrCount() // 2
+		 *		nodeList.get( 1 ).getAttrCount() // 2
+		 *		nodeList.get( 2 ).getAttrCount() // 2
 		 *
 		 *		var nodeListA = new NodeList( 'foo' );
 		 *		var nodeListB = new NodeList( nodeListA );

+ 22 - 22
packages/ckeditor5-utils/tests/document/changeoperation.js

@@ -42,7 +42,7 @@ describe( 'ChangeOperation', function() {
 		expect( doc.root.getChildCount() ).to.be.equal( 3 );
 		expect( doc.root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
 		expect( doc.root.getChild( 1 ).hasAttr( newAttr ) ).to.be.true;
-		expect( doc.root.getChild( 2 ).attrs.length ).to.be.equal( 0 );
+		expect( doc.root.getChild( 2 ).getAttrCount() ).to.be.equal( 0 );
 	} );
 
 	it( 'should insert attribute to multiple ranges', function() {
@@ -70,7 +70,7 @@ describe( 'ChangeOperation', function() {
 		expect( doc.version ).to.be.equal( 1 );
 		expect( doc.root.getChildCount() ).to.be.equal( 3 );
 		expect( doc.root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
-		expect( doc.root.getChild( 1 ).attrs.length ).to.be.equal( 0 );
+		expect( doc.root.getChild( 1 ).getAttrCount() ).to.be.equal( 0 );
 		expect( doc.root.getChild( 2 ).hasAttr( newAttr ) ).to.be.true;
 	} );
 
@@ -98,7 +98,7 @@ describe( 'ChangeOperation', function() {
 
 		expect( doc.version ).to.be.equal( 1 );
 		expect( doc.root.getChildCount() ).to.be.equal( 1 );
-		expect( doc.root.getChild( 0 ).attrs.length ).to.be.equal( 3 );
+		expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 3 );
 		expect( doc.root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
 		expect( doc.root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
 		expect( doc.root.getChild( 0 ).hasAttr( barAttr ) ).to.be.true;
@@ -130,11 +130,11 @@ describe( 'ChangeOperation', function() {
 
 		expect( doc.version ).to.be.equal( 1 );
 		expect( doc.root.getChildCount() ).to.be.equal( 3 );
-		expect( doc.root.getChild( 0 ).attrs.length ).to.be.equal( 1 );
+		expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 1 );
 		expect( doc.root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
-		expect( doc.root.getChild( 1 ).attrs.length ).to.be.equal( 1 );
+		expect( doc.root.getChild( 1 ).getAttrCount() ).to.be.equal( 1 );
 		expect( doc.root.getChild( 1 ).hasAttr( oldAttr ) ).to.be.true;
-		expect( doc.root.getChild( 2 ).attrs.length ).to.be.equal( 1 );
+		expect( doc.root.getChild( 2 ).getAttrCount() ).to.be.equal( 1 );
 		expect( doc.root.getChild( 2 ).hasAttr( newAttr ) ).to.be.true;
 	} );
 
@@ -161,11 +161,11 @@ describe( 'ChangeOperation', function() {
 
 		expect( doc.version ).to.be.equal( 1 );
 		expect( doc.root.getChildCount() ).to.be.equal( 3 );
-		expect( doc.root.getChild( 0 ).attrs.length ).to.be.equal( 1 );
+		expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 1 );
 		expect( doc.root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
-		expect( doc.root.getChild( 1 ).attrs.length ).to.be.equal( 1 );
+		expect( doc.root.getChild( 1 ).getAttrCount() ).to.be.equal( 1 );
 		expect( doc.root.getChild( 1 ).hasAttr( newAttr ) ).to.be.true;
-		expect( doc.root.getChild( 2 ).attrs.length ).to.be.equal( 1 );
+		expect( doc.root.getChild( 2 ).getAttrCount() ).to.be.equal( 1 );
 		expect( doc.root.getChild( 2 ).hasAttr( oldAttr ) ).to.be.true;
 	} );
 
@@ -194,7 +194,7 @@ describe( 'ChangeOperation', function() {
 
 		expect( doc.version ).to.be.equal( 1 );
 		expect( doc.root.getChildCount() ).to.be.equal( 1 );
-		expect( doc.root.getChild( 0 ).attrs.length ).to.be.equal( 3 );
+		expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 3 );
 		expect( doc.root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
 		expect( doc.root.getChild( 0 ).hasAttr( x2Attr ) ).to.be.true;
 		expect( doc.root.getChild( 0 ).hasAttr( barAttr ) ).to.be.true;
@@ -224,7 +224,7 @@ describe( 'ChangeOperation', function() {
 
 		expect( doc.version ).to.be.equal( 1 );
 		expect( doc.root.getChildCount() ).to.be.equal( 1 );
-		expect( doc.root.getChild( 0 ).attrs.length ).to.be.equal( 2 );
+		expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 2 );
 		expect( doc.root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
 		expect( doc.root.getChild( 0 ).hasAttr( barAttr ) ).to.be.true;
 	} );
@@ -254,9 +254,9 @@ describe( 'ChangeOperation', function() {
 
 		expect( doc.version ).to.be.equal( 1 );
 		expect( doc.root.getChildCount() ).to.be.equal( 3 );
-		expect( doc.root.getChild( 0 ).attrs.length ).to.be.equal( 0 );
+		expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 0 );
 		expect( doc.root.getChild( 1 ).hasAttr( fooAttr ) ).to.be.true;
-		expect( doc.root.getChild( 2 ).attrs.length ).to.be.equal( 0 );
+		expect( doc.root.getChild( 2 ).getAttrCount() ).to.be.equal( 0 );
 	} );
 
 	it( 'should create a change operation as a reverse', function() {
@@ -311,9 +311,9 @@ describe( 'ChangeOperation', function() {
 
 		expect( doc.version ).to.be.equal( 2 );
 		expect( doc.root.getChildCount() ).to.be.equal( 3 );
-		expect( doc.root.getChild( 0 ).attrs.length ).to.be.equal( 0 );
-		expect( doc.root.getChild( 1 ).attrs.length ).to.be.equal( 0 );
-		expect( doc.root.getChild( 2 ).attrs.length ).to.be.equal( 0 );
+		expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 0 );
+		expect( doc.root.getChild( 1 ).getAttrCount() ).to.be.equal( 0 );
+		expect( doc.root.getChild( 2 ).getAttrCount() ).to.be.equal( 0 );
 	} );
 
 	it( 'should undo change attribute by applying reverse operation', function() {
@@ -345,11 +345,11 @@ describe( 'ChangeOperation', function() {
 
 		expect( doc.version ).to.be.equal( 2 );
 		expect( doc.root.getChildCount() ).to.be.equal( 3 );
-		expect( doc.root.getChild( 0 ).attrs.length ).to.be.equal( 1 );
+		expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 1 );
 		expect( doc.root.getChild( 0 ).hasAttr( oldAttr ) ).to.be.true;
-		expect( doc.root.getChild( 1 ).attrs.length ).to.be.equal( 1 );
+		expect( doc.root.getChild( 1 ).getAttrCount() ).to.be.equal( 1 );
 		expect( doc.root.getChild( 1 ).hasAttr( oldAttr ) ).to.be.true;
-		expect( doc.root.getChild( 2 ).attrs.length ).to.be.equal( 1 );
+		expect( doc.root.getChild( 2 ).getAttrCount() ).to.be.equal( 1 );
 		expect( doc.root.getChild( 2 ).hasAttr( oldAttr ) ).to.be.true;
 	} );
 
@@ -381,11 +381,11 @@ describe( 'ChangeOperation', function() {
 
 		expect( doc.version ).to.be.equal( 2 );
 		expect( doc.root.getChildCount() ).to.be.equal( 3 );
-		expect( doc.root.getChild( 0 ).attrs.length ).to.be.equal( 1 );
+		expect( doc.root.getChild( 0 ).getAttrCount() ).to.be.equal( 1 );
 		expect( doc.root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
-		expect( doc.root.getChild( 1 ).attrs.length ).to.be.equal( 1 );
+		expect( doc.root.getChild( 1 ).getAttrCount() ).to.be.equal( 1 );
 		expect( doc.root.getChild( 1 ).hasAttr( fooAttr ) ).to.be.true;
-		expect( doc.root.getChild( 2 ).attrs.length ).to.be.equal( 1 );
+		expect( doc.root.getChild( 2 ).getAttrCount() ).to.be.equal( 1 );
 		expect( doc.root.getChild( 2 ).hasAttr( fooAttr ) ).to.be.true;
 	} );
 

+ 4 - 4
packages/ckeditor5-utils/tests/document/character.js

@@ -27,7 +27,7 @@ describe( 'constructor', function() {
 		expect( character ).to.be.an.instanceof( Node );
 		expect( character ).to.have.property( 'character' ).that.equals( 'f' );
 		expect( character ).to.have.property( 'parent' ).that.equals( parent );
-		expect( character ).to.have.property( 'attrs' ).that.is.an( 'array' ).and.is.empty;
+		expect( character.getAttrCount() ).to.equals( 0 );
 	} );
 
 	it( 'should create character with attributes', function() {
@@ -36,7 +36,7 @@ describe( 'constructor', function() {
 		var Node = modules[ 'document/node' ];
 		var Attribute = modules[ 'document/attribute' ];
 
-		var attr = new Attribute( 'key', 'value' );
+		var attr = new Attribute( 'foo', 'bar' );
 
 		var character = new Character( 'f', [ attr ] );
 
@@ -45,7 +45,7 @@ describe( 'constructor', function() {
 		expect( character ).to.be.an.instanceof( Node );
 		expect( character ).to.have.property( 'character' ).that.equals( 'f' );
 		expect( character ).to.have.property( 'parent' ).that.equals( parent );
-		expect( character ).to.have.property( 'attrs' ).that.is.an( 'array' ).with.length( 1 );
-		expect( character.attrs[ 0 ] ).that.equals( attr );
+		expect( character.getAttrCount() ).to.be.equals( 1 );
+		expect( character.getAttr( attr.key ) ).to.equals( attr.value );
 	} );
 } );

+ 4 - 4
packages/ckeditor5-utils/tests/document/element.js

@@ -25,7 +25,7 @@ describe( 'constructor', function() {
 		expect( element ).to.be.an.instanceof( Node );
 		expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
 		expect( element ).to.have.property( 'parent' ).that.equals( parent );
-		expect( element ).to.have.property( 'attrs' ).that.is.an( 'array' ).and.is.empty;
+		expect( element.getAttrCount() ).to.equals( 0 );
 	} );
 
 	it( 'should create element with attributes', function() {
@@ -33,7 +33,7 @@ describe( 'constructor', function() {
 		var Node = modules[ 'document/node' ];
 		var Attribute = modules[ 'document/attribute' ];
 
-		var attr = new Attribute( 'key', 'value' );
+		var attr = new Attribute( 'foo', 'bar' );
 
 		var element = new Element( 'elem', [ attr ] );
 
@@ -42,8 +42,8 @@ describe( 'constructor', function() {
 		expect( element ).to.be.an.instanceof( Node );
 		expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
 		expect( element ).to.have.property( 'parent' ).that.equals( parent );
-		expect( element ).to.have.property( 'attrs' ).that.is.an( 'array' ).with.length( 1 );
-		expect( element.attrs[ 0 ] ).that.equals( attr );
+		expect( element.getAttrCount() ).to.equals( 1 );
+		expect( element.getAttr( attr.key ) ).to.equals( attr.value );
 	} );
 
 	it( 'should create element with children', function() {

+ 3 - 24
packages/ckeditor5-utils/tests/document/node.js

@@ -110,7 +110,7 @@ describe( 'getAttr', function() {
 		var fooAttr = new Attribute( 'foo', true );
 		var element = new Element( 'foo', [ fooAttr ] );
 
-		expect( element.getAttr( 'foo' ).isEqual( fooAttr ) ).to.be.true;
+		expect( element.getAttr( 'foo' ) ).to.equals( fooAttr.value );
 	} );
 
 	it( 'should return null if attribute was not found by key', function() {
@@ -122,27 +122,6 @@ describe( 'getAttr', function() {
 
 		expect( element.getAttr( 'bar' ) ).to.be.null;
 	} );
-
-	it( 'should be possible to get attribute by object', function() {
-		var Element = modules[ 'document/element' ];
-		var Attribute = modules[ 'document/attribute' ];
-
-		var fooAttr = new Attribute( 'foo', true );
-		var foo2Attr = new Attribute( 'foo', true );
-		var element = new Element( 'foo', [ fooAttr ] );
-
-		expect( element.getAttr( foo2Attr ).isEqual( fooAttr ) ).to.be.true;
-	} );
-
-	it( 'should return null if attribute was not found by object', function() {
-		var Element = modules[ 'document/element' ];
-		var Attribute = modules[ 'document/attribute' ];
-
-		var fooAttr = new Attribute( 'foo', true );
-		var element = new Element( 'foo' );
-
-		expect( element.getAttr( fooAttr ) ).to.be.null;
-	} );
 } );
 
 describe( 'hasAttr', function() {
@@ -203,14 +182,14 @@ describe( 'hasAttr', function() {
 		expect( parsedFoo ).to.be.deep.equals( {
 			name: 'foo',
 			parent: null,
-			attrs: [],
+			_attrs: [],
 			_children: new NodeList( parsedFoo._children )
 		} );
 
 		expect( parsedBar ).to.be.deep.equals( {
 			character: 'b',
 			parent: 'foo',
-			attrs: []
+			_attrs: []
 		} );
 	} );
 } );

+ 5 - 8
packages/ckeditor5-utils/tests/document/nodelist.js

@@ -56,20 +56,17 @@ describe( 'constructor', function() {
 		var Text = modules[ 'document/text' ];
 		var Attribute = modules[ 'document/attribute' ];
 
-		var attrs = [ new Attribute( 'bold', true ) ];
+		var attr = new Attribute( 'bold', true );
 
-		var nodeList = new NodeList( new Text( 'foo', attrs ) );
+		var nodeList = new NodeList( new Text( 'foo', [ attr ] ) );
 
 		expect( nodeList.length ).to.be.equal( 3 );
 		expect( nodeList.get( 0 ).character ).to.be.equal( 'f' );
-		expect( nodeList.get( 0 ).attrs ).to.be.deep.equal( attrs );
-		expect( nodeList.get( 0 ).attrs ).not.to.be.equal( attrs );
+		expect( nodeList.get( 0 ).getAttr( attr.key ) ).to.be.equal( attr.value );
 		expect( nodeList.get( 1 ).character ).to.be.equal( 'o' );
-		expect( nodeList.get( 1 ).attrs ).to.be.deep.equal( attrs );
-		expect( nodeList.get( 1 ).attrs ).not.to.be.equal( attrs );
+		expect( nodeList.get( 1 ).getAttr( attr.key ) ).to.be.equal( attr.value );
 		expect( nodeList.get( 2 ).character ).to.be.equal( 'o' );
-		expect( nodeList.get( 2 ).attrs ).to.be.deep.equal( attrs );
-		expect( nodeList.get( 2 ).attrs ).not.to.be.equal( attrs );
+		expect( nodeList.get( 2 ).getAttr( attr.key ) ).to.be.equal( attr.value );
 	} );
 } );