8
0
Просмотр исходного кода

Removed `setAttributesTo` method from the Selection API.

Maciej Bukowski 8 лет назад
Родитель
Сommit
63e1f11459

+ 4 - 1
packages/ckeditor5-engine/src/dev-utils/model.js

@@ -307,7 +307,10 @@ export function parse( data, schema, options = {} ) {
 
 		// Set attributes to selection if specified.
 		if ( options.selectionAttributes ) {
-			selection.setAttributesTo( options.selectionAttributes );
+			for ( const key in options.selectionAttributes ) {
+				const value = options.selectionAttributes[ key ];
+				selection.setAttribute( key, value );
+			}
 		}
 	}
 

+ 2 - 6
packages/ckeditor5-engine/src/model/documentselection.js

@@ -357,15 +357,11 @@ export default class DocumentSelection {
 		this._selection.clearAttributes();
 	}
 
-	_getStoredAttributes() {
-		return this._selection._getStoredAttributes();
-	}
-
 	/**
 	 * @protected
 	*/
-	_setAttributesTo( attrs ) {
-		return this._selection.setAttributesTo( attrs );
+	_getStoredAttributes() {
+		return this._selection._getStoredAttributes();
 	}
 
 	/**

+ 4 - 13
packages/ckeditor5-engine/src/model/liveselection.js

@@ -238,14 +238,12 @@ export default class LiveSelection extends Selection {
 	/**
 	 * @inheritDoc
 	 */
-	setAttributesTo( attrs ) {
-		attrs = toMap( attrs );
-
+	clearAttributes() {
 		if ( this.isCollapsed && this.anchor.parent.isEmpty ) {
-			this._setStoredAttributesTo( attrs );
+			this._setStoredAttributesTo( [] );
 		}
 
-		const changed = this._setAttributesTo( attrs );
+		const changed = this._setAttributesTo( new Map() );
 
 		if ( changed.size > 0 ) {
 			// Fire event with exact data (fire only if anything changed).
@@ -254,13 +252,6 @@ export default class LiveSelection extends Selection {
 		}
 	}
 
-	/**
-	 * @inheritDoc
-	 */
-	clearAttributes() {
-		this.setAttributesTo( [] );
-	}
-
 	/**
 	 * Removes all attributes from the selection and sets attributes according to the surrounding nodes.
 	 *
@@ -488,7 +479,7 @@ export default class LiveSelection extends Selection {
 	 * `directChange` parameter).
 	 *
 	 * @private
-	 * @param {Iterable|Object} attrs Iterable object containing attributes to be set.
+	 * @param {Map.<String,*>} attrs Iterable object containing attributes to be set.
 	 * @param {Boolean} [directChange=true] `true` if the change is caused by `Selection` API, `false` if change
 	 * is caused by `Batch` API.
 	 * @returns {Set.<String>} Changed attribute keys.

+ 0 - 31
packages/ckeditor5-engine/src/model/selection.js

@@ -13,8 +13,6 @@ import Range from './range';
 import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
-import toMap from '@ckeditor/ckeditor5-utils/src/tomap';
-import mapsEqual from '@ckeditor/ckeditor5-utils/src/mapsequal';
 import isIterable from '@ckeditor/ckeditor5-utils/src/isiterable';
 import DocumentSelection from './documentselection';
 
@@ -512,35 +510,6 @@ export default class Selection {
 		}
 	}
 
-	/**
-	 * Removes all attributes from the selection and sets given attributes.
-	 *
-	 * If given set of attributes is different than set of attributes already added to selection, fires
-	 * {@link #event:change change event} with keys of attributes that changed.
-	 *
-	 * @fires event:change:attribute
-	 * @param {Iterable|Object} attrs Iterable object containing attributes to be set.
-	 */
-	setAttributesTo( attrs ) {
-		attrs = toMap( attrs );
-
-		if ( !mapsEqual( attrs, this._attrs ) ) {
-			// Create a set from keys of old and new attributes.
-			const changed = new Set( Array.from( attrs.keys() ).concat( Array.from( this._attrs.keys() ) ) );
-
-			for ( const [ key, value ] of attrs ) {
-				// If the attribute remains unchanged, remove it from changed set.
-				if ( this._attrs.get( key ) === value ) {
-					changed.delete( key );
-				}
-			}
-
-			this._attrs = attrs;
-
-			this.fire( 'change:attribute', { attributeKeys: Array.from( changed ), directChange: true } );
-		}
-	}
-
 	/**
 	 * Returns the selected element. {@link module:engine/model/element~Element Element} is considered as selected if there is only
 	 * one range in the selection, and that range contains exactly one element.

+ 0 - 38
packages/ckeditor5-engine/tests/model/documentselection.js

@@ -723,44 +723,6 @@ describe( 'DocumentSelection', () => {
 			} );
 		} );
 
-		describe( '_setAttributesTo()', () => {
-			it( 'should fire change:attribute event with correct parameters', done => {
-				selection._setAttributesTo( { foo: 'bar', abc: 'def' } );
-
-				selection.on( 'change:attribute', ( evt, data ) => {
-					expect( data.directChange ).to.be.true;
-					expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
-
-					done();
-				} );
-
-				selection._setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
-			} );
-
-			it( 'should not fire change:attribute event if same attributes are set', () => {
-				selection._setAttributesTo( { foo: 'bar', abc: 'def' } );
-
-				const spy = sinon.spy();
-				selection.on( 'change:attribute', spy );
-
-				selection._setAttributesTo( { foo: 'bar', abc: 'def' } );
-
-				expect( spy.called ).to.be.false;
-			} );
-
-			it( 'should remove all stored attributes and store the given ones if the selection is in empty node', () => {
-				selection._setTo( [ rangeInEmptyP ] );
-				selection._setAttribute( 'abc', 'xyz' );
-				selection._setAttributesTo( { foo: 'bar' } );
-
-				expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
-				expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
-
-				expect( emptyP.getAttribute( fooStoreAttrKey ) ).to.equal( 'bar' );
-				expect( emptyP.hasAttribute( abcStoreAttrKey ) ).to.be.false;
-			} );
-		} );
-
 		describe( 'removeAttribute()', () => {
 			it( 'should remove attribute set on the text fragment', () => {
 				selection._setTo( [ rangeInFullP ] );

+ 0 - 44
packages/ckeditor5-engine/tests/model/selection.js

@@ -1171,50 +1171,6 @@ describe( 'Selection', () => {
 				expect( spy.called ).to.be.false;
 			} );
 		} );
-
-		describe( 'setAttributesTo()', () => {
-			it( 'should remove all attributes set on element and set the given ones', () => {
-				selection.setAttribute( 'abc', 'xyz' );
-				selection.setAttributesTo( { foo: 'bar' } );
-
-				expect( selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
-				expect( selection.getAttribute( 'abc' ) ).to.be.undefined;
-			} );
-
-			it( 'should fire only one change:attribute event', () => {
-				selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
-
-				const spy = sinon.spy();
-				selection.on( 'change:attribute', spy );
-
-				selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
-
-				expect( spy.calledOnce ).to.be.true;
-			} );
-
-			it( 'should fire change:attribute event with correct parameters', () => {
-				selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
-
-				selection.on( 'change:attribute', ( evt, data ) => {
-					expect( data.directChange ).to.be.true;
-					expect( data.attributeKeys ).to.deep.equal( [ 'abc', 'xxx' ] );
-				} );
-
-				selection.setAttributesTo( { foo: 'bar', abc: 'def' } );
-			} );
-
-			it( 'should not fire change:attribute event if attributes had not changed', () => {
-				selection.setTo( [ rangeInFullP ] );
-				selection.setAttributesTo( { foo: 'bar', xxx: 'yyy' } );
-
-				const spy = sinon.spy();
-				selection.on( 'change:attribute', spy );
-
-				selection.setAttributesTo( { xxx: 'yyy', foo: 'bar' } );
-
-				expect( spy.called ).to.be.false;
-			} );
-		} );
 	} );
 
 	describe( 'containsEntireContent()', () => {