Explorar o código

Feature: Introduced schema#setAttributeProperties() and schema#getAttributeProperties() methods.

Marek Lewandowski %!s(int64=6) %!d(string=hai) anos
pai
achega
b9efcbe7d4

+ 38 - 0
packages/ckeditor5-engine/src/model/schema.js

@@ -39,6 +39,14 @@ export default class Schema {
 	constructor() {
 		this._sourceDefinitions = {};
 
+		/**
+		 * A map containing attribute's properties.
+		 *
+		 * @private
+		 * @member {Map.<String,String>}
+		 */
+		this._attributeProperties = {};
+
 		this.decorate( 'checkChild' );
 		this.decorate( 'checkAttribute' );
 
@@ -140,6 +148,36 @@ export default class Schema {
 		this._clearCache();
 	}
 
+	/**
+	 * Registers custom properties to a given attribute.
+	 *
+	 *		// Mark blockQuote as a formatting attribute.
+	 *		schema.setAttributeProperties( 'blockQuote', {
+	 *			isFormatting: true
+	 *		} );
+	 *
+	 *		// Override code not to be considered a formatting markup.
+	 *		schema.setAttributeProperties( 'code', {
+	 *			isFormatting: false
+	 *		} );
+	 *
+	 * @param {String} attributeName Name of the attribute to receive properties.
+	 * @param {Map.<String,String>} properties Dictionary of properties.
+	 */
+	setAttributeProperties( attributeName, properties ) {
+		this._attributeProperties[ attributeName ] = Object.assign( this._attributeProperties[ attributeName ] || {}, properties );
+	}
+
+	/**
+	 * Returns properties assigned to a given attribute.
+	 *
+	 * @param {String} attributeName Name of the attribute.
+	 * @returns {Map.<String,String>}
+	 */
+	getAttributeProperties( attributeName ) {
+		return this._attributeProperties[ attributeName ];
+	}
+
 	/**
 	 * Returns all registered items.
 	 *

+ 43 - 0
packages/ckeditor5-engine/tests/model/schema.js

@@ -96,6 +96,49 @@ describe( 'Schema', () => {
 		} );
 	} );
 
+	describe( 'setAttributeProperties()', () => {
+		beforeEach( () => {
+			schema.register( '$root' );
+			schema.register( 'paragraph', {
+				allowIn: '$root'
+			} );
+			schema.register( '$text', {
+				allowIn: 'paragraph'
+			} );
+		} );
+
+		it( 'allows registering new properties', () => {
+			schema.extend( '$text', { allowAttributes: 'testAttribute' } );
+
+			schema.setAttributeProperties( 'testAttribute', {
+				foo: 'bar',
+				baz: 'bom'
+			} );
+
+			expect( schema.getAttributeProperties( 'testAttribute' ) ).to.deep.equal( {
+				foo: 'bar',
+				baz: 'bom'
+			} );
+		} );
+
+		it( 'support adding properties in subsequent calls', () => {
+			schema.extend( '$text', { allowAttributes: 'testAttribute' } );
+
+			schema.setAttributeProperties( 'testAttribute', {
+				first: 'foo'
+			} );
+
+			schema.setAttributeProperties( 'testAttribute', {
+				second: 'bar'
+			} );
+
+			expect( schema.getAttributeProperties( 'testAttribute' ) ).to.deep.equal( {
+				first: 'foo',
+				second: 'bar'
+			} );
+		} );
+	} );
+
 	describe( 'getDefinitions()', () => {
 		it( 'returns compiled definitions', () => {
 			schema.register( '$root' );