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

Added: setting attributes API in engine.treeModel.CharacterProxy.

Szymon Cofalik 9 лет назад
Родитель
Сommit
ec83c33428

+ 63 - 0
packages/ckeditor5-engine/src/treemodel/characterproxy.js

@@ -6,6 +6,7 @@
 'use strict';
 
 import Node from './node.js';
+import utils from '../../utils/utils.js';
 
 /**
  * A proxy object representing one character stored in the tree data model. It looks and behaves like
@@ -68,4 +69,66 @@ export default class CharacterProxy extends Node {
 		 */
 		this._index = index;
 	}
+
+	/**
+	 * Sets attribute on the text fragment. If attribute with the same key already is set, it overwrites its values.
+	 *
+	 * **Note:** Changing attributes of text fragment affects document state. This TextProxy instance properties
+	 * will be refreshed, but other may get invalidated. It is highly unrecommended to store references to TextProxy instances.
+	 *
+	 * @param {String} key Key of attribute to set.
+	 * @param {*} value Attribute value.
+	 */
+	setAttribute( key, value ) {
+		let index = this.getIndex();
+
+		this.parent._children.setAttribute( index, 1, key, value );
+		this._attrs.set( key, value );
+	}
+
+	/**
+	 * Removes all attributes from the character proxy and sets given attributes.
+	 *
+	 * **Note:** Changing attributes of character proxy affects document state. This `CharacterProxy` instance properties
+	 * will be refreshed, but other instances of `CharacterProxy` and `TextProxy` may get invalidated.
+	 * It is highly unrecommended to store references to `CharacterProxy` instances.
+	 *
+	 * @param {Iterable|Object} attrs Iterable object containing attributes to be set. See
+	 * {@link engine.treeModel.CharacterProxy#getAttributes}.
+	 */
+	setAttributesTo( attrs ) {
+		let attrsMap = utils.toMap( attrs );
+
+		this.clearAttributes();
+
+		for ( let attr of attrsMap ) {
+			this.setAttribute( attr[ 0 ], attr[ 1 ] );
+		}
+	}
+
+	/**
+	 * Removes an attribute with given key from the character proxy.
+	 *
+	 * **Note:** Changing attributes of character proxy affects document state. This `CharacterProxy` instance properties
+	 * will be refreshed, but other instances of `CharacterProxy` and `TextProxy` may get invalidated.
+	 * It is highly unrecommended to store references to `CharacterProxy` instances.
+	 *
+	 * @param {String} key Key of attribute to remove.
+	 */
+	removeAttribute( key ) {
+		this.setAttribute( key, null );
+	}
+
+	/**
+	 * Removes all attributes from the character proxy.
+	 *
+	 * **Note:** Changing attributes of character proxy affects document state. This `CharacterProxy` instance properties
+	 * will be refreshed, but other instances of `CharacterProxy` and `TextProxy` may get invalidated.
+	 * It is highly unrecommended to store references to `CharacterProxy` instances.
+	 */
+	clearAttributes() {
+		for ( let attr of this.getAttributes() ) {
+			this.removeAttribute( attr[ 0 ] );
+		}
+	}
 }

+ 93 - 4
packages/ckeditor5-engine/tests/treemodel/characterproxy.js

@@ -15,12 +15,9 @@ import utils from '/ckeditor5/utils/utils.js';
 describe( 'CharacterProxy', () => {
 	let text, element, char;
 
-	before( () => {
+	beforeEach( () => {
 		text = new Text( 'abc', { foo: true } );
 		element = new Element( 'div', [], [ new Element( 'p' ), text, new Element( 'p' ) ] );
-	} );
-
-	beforeEach( () => {
 		char = element.getChild( 2 );
 	} );
 
@@ -43,4 +40,96 @@ describe( 'CharacterProxy', () => {
 	it( 'should return correct index in parent node', () => {
 		expect( char.getIndex() ).to.equal( 2 );
 	} );
+
+	describe( 'attributes interface', () => {
+		describe( 'hasAttribute', () => {
+			it( 'should return true if text fragment has attribute with given key', () => {
+				expect( char.hasAttribute( 'foo' ) ).to.be.true;
+			} );
+
+			it( 'should return false if text fragment does not have attribute with given key', () => {
+				expect( char.hasAttribute( 'abc' ) ).to.be.false;
+			} );
+		} );
+
+		describe( 'getAttribute', () => {
+			it( 'should return attribute with given key if text fragment has given attribute', () => {
+				expect( char.getAttribute( 'foo' ) ).to.be.true;
+			} );
+
+			it( 'should return undefined if text fragment does not have given attribute', () => {
+				expect( char.getAttribute( 'bar' ) ).to.be.undefined;
+			} );
+		} );
+
+		describe( 'getAttributes', () => {
+			it( 'should return an iterator that iterates over all attributes set on the text fragment', () => {
+				let attrs = Array.from( char.getAttributes() );
+
+				expect( attrs ).to.deep.equal( [ [ 'foo', true ] ] );
+			} );
+		} );
+
+		describe( 'setAttribute', () => {
+			it( 'should set attribute on given character', () => {
+				char.setAttribute( 'abc', 'xyz' );
+
+				expect( element.getChild( 0 ).getAttribute( 'abc' ) ).to.be.undefined;
+				expect( element.getChild( 1 ).getAttribute( 'abc' ) ).to.be.undefined;
+				expect( element.getChild( 2 ).getAttribute( 'abc' ) ).to.equal( 'xyz' );
+				expect( element.getChild( 3 ).getAttribute( 'abc' ) ).to.be.undefined;
+				expect( element.getChild( 4 ).getAttribute( 'abc' ) ).to.be.undefined;
+			} );
+
+			it( 'should remove attribute when passed attribute value is null', () => {
+				char.setAttribute( 'foo', null );
+
+				expect( element.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.false;
+				expect( element.getChild( 1 ).hasAttribute( 'foo' ) ).to.be.true;
+				expect( element.getChild( 2 ).hasAttribute( 'foo' ) ).to.be.false;
+				expect( element.getChild( 3 ).hasAttribute( 'foo' ) ).to.be.true;
+				expect( element.getChild( 4 ).hasAttribute( 'foo' ) ).to.be.false;
+			} );
+
+			it( 'should correctly split and merge characters', () => {
+				char.setAttribute( 'abc', 'xyz' );
+				char.nextSibling.setAttribute( 'abc', 'xyz' );
+
+				expect( element._children._nodes.length ).to.equal( 4 );
+				expect( element._children._nodes[ 1 ].text ).to.equal( 'a' );
+				expect( element._children._nodes[ 2 ].text ).to.equal( 'bc' );
+			} );
+		} );
+
+		describe( 'setAttributesTo', () => {
+			it( 'should remove all attributes from character and set given ones', () => {
+				char.setAttributesTo( { abc: 'xyz' } );
+
+				expect( element.getChild( 2 ).hasAttribute( 'foo' ) ).to.be.false;
+				expect( element.getChild( 2 ).getAttribute( 'abc' ) ).to.equal( 'xyz' );
+			} );
+		} );
+
+		describe( 'removeAttribute', () => {
+			it( 'should remove given attribute from character', () => {
+				char.removeAttribute( 'foo' );
+
+				expect( element.getChild( 0 ).hasAttribute( 'foo' ) ).to.be.false;
+				expect( element.getChild( 1 ).hasAttribute( 'foo' ) ).to.be.true;
+				expect( element.getChild( 2 ).hasAttribute( 'foo' ) ).to.be.false;
+				expect( element.getChild( 3 ).hasAttribute( 'foo' ) ).to.be.true;
+				expect( element.getChild( 4 ).hasAttribute( 'foo' ) ).to.be.false;
+			} );
+		} );
+
+		describe( 'clearAttributes', () => {
+			it( 'should remove all attributes from text fragment', () => {
+				char.setAttribute( 'abc', 'xyz' );
+				char.clearAttributes();
+
+				expect( element.getChild( 2 ).hasAttribute( 'foo' ) ).to.be.false;
+				expect( element.getChild( 2 ).hasAttribute( 'abc' ) ).to.be.false;
+			} );
+		} );
+	} );
 } );