Parcourir la source

Added: Attributes interface for engine.treeModel.Text.

Szymon Cofalik il y a 9 ans
Parent
commit
258dea00dd

+ 65 - 0
packages/ckeditor5-engine/src/treemodel/text.js

@@ -39,4 +39,69 @@ export default class Text {
 		 */
 		this._attrs = utils.toMap( attrs );
 	}
+
+	/**
+	 * Checks if the text has an attribute for given key.
+	 *
+	 * @param {String} key Key of attribute to check.
+	 * @returns {Boolean} `true` if attribute with given key is set on text, `false` otherwise.
+	 */
+	hasAttribute( key ) {
+		return this._attrs.has( key );
+	}
+
+	/**
+	 * Gets an attribute value for given key or undefined if that attribute is not set on text.
+	 *
+	 * @param {String} key Key of attribute to look for.
+	 * @returns {*} Attribute value or null.
+	 */
+	getAttribute( key ) {
+		return this._attrs.get( key );
+	}
+
+	/**
+	 * Returns iterator that iterates over this text attributes.
+	 *
+	 * @returns {Iterable.<*>}
+	 */
+	getAttributes() {
+		return this._attrs[ Symbol.iterator ]();
+	}
+
+	/**
+	 * Sets attribute on text. If attribute with the same key already is set, it overwrites its value.
+	 *
+	 * @param {String} key Key of attribute to set.
+	 * @param {*} value Attribute value.
+	 */
+	setAttribute( key, value ) {
+		this._attrs.set( key, value );
+	}
+
+	/**
+	 * Removes all attributes from text and sets given attributes.
+	 *
+	 * @param {Iterable|Object} attrs Iterable object containing attributes to be set. See {@link engine.treeModel.Text#getAttributes}.
+	 */
+	setAttributesTo( attrs ) {
+		this._attrs = utils.toMap( attrs );
+	}
+
+	/**
+	 * Removes an attribute with given key from text.
+	 *
+	 * @param {String} key Key of attribute to remove.
+	 * @returns {Boolean} `true` if the attribute was set on text, `false` otherwise.
+	 */
+	removeAttribute( key ) {
+		return this._attrs.delete( key );
+	}
+
+	/**
+	 * Removes all attributes from text.
+	 */
+	clearAttributes() {
+		this._attrs.clear();
+	}
 }

+ 79 - 0
packages/ckeditor5-engine/tests/treemodel/text.js

@@ -27,4 +27,83 @@ describe( 'Text', () => {
 			expect( empty2.text ).to.equal( '' );
 		} );
 	} );
+
+	describe( 'attributes interface', () => {
+		let text;
+
+		beforeEach( () => {
+			text = new Text( 'bar', { foo: 'bar' } );
+		} );
+
+		describe( 'hasAttribute', () => {
+			it( 'should return true if element contains attribute with given key', () => {
+				expect( text.hasAttribute( 'foo' ) ).to.be.true;
+			} );
+
+			it( 'should return false if element does not contain attribute with given key', () => {
+				expect( text.hasAttribute( 'bar' ) ).to.be.false;
+			} );
+		} );
+
+		describe( 'getAttribute', () => {
+			it( 'should return attribute value for given key if element contains given attribute', () => {
+				expect( text.getAttribute( 'foo' ) ).to.equal( 'bar' );
+			} );
+
+			it( 'should return undefined if element does not contain given attribute', () => {
+				expect( text.getAttribute( 'bar' ) ).to.be.undefined;
+			} );
+		} );
+
+		describe( 'getAttributes', () => {
+			it( 'should return an iterator that iterates over all attributes set on the element', () => {
+				expect( Array.from( text.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
+			} );
+		} );
+
+		describe( 'setAttribute', () => {
+			it( 'should set given attribute on the element', () => {
+				text.setAttribute( 'abc', 'xyz' );
+
+				expect( text.getAttribute( 'abc' ) ).to.equal( 'xyz' );
+			} );
+		} );
+
+		describe( 'setAttributesTo', () => {
+			it( 'should remove all attributes set on element and set the given ones', () => {
+				text.setAttribute( 'abc', 'xyz' );
+				text.setAttributesTo( { bold: true } );
+
+				expect( text.getAttribute( 'bold' ) ).to.equal( true );
+				expect( text.getAttribute( 'foo' ) ).to.be.undefined;
+				expect( text.getAttribute( 'abc' ) ).to.be.undefined;
+			} );
+		} );
+
+		describe( 'removeAttribute', () => {
+			it( 'should remove attribute set on the element and return true', () => {
+				let result = text.removeAttribute( 'foo' );
+
+				expect( text.getAttribute( 'foo' ) ).to.be.undefined;
+				expect( result ).to.be.true;
+			} );
+
+			it( 'should return false if element does not contain given attribute', () => {
+				let result = text.removeAttribute( 'abc' );
+
+				expect( result ).to.be.false;
+			} );
+		} );
+
+		describe( 'clearAttributes', () => {
+			it( 'should remove all attributes from the element', () => {
+				text.setAttribute( 'abc', 'xyz' );
+
+				text.clearAttributes();
+
+				expect( text.getAttribute( 'foo' ) ).to.be.undefined;
+				expect( text.getAttribute( 'abc' ) ).to.be.undefined;
+			} );
+		} );
+	} );
 } );