Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge 118b34c into c0688d1
Browse files Browse the repository at this point in the history
  • Loading branch information
mlewand committed Mar 28, 2019
2 parents c0688d1 + 118b34c commit bd25c72
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 13 deletions.
94 changes: 81 additions & 13 deletions src/model/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

Expand Down Expand Up @@ -140,19 +148,6 @@ export default class Schema {
this._clearCache();
}

/**
* Returns all registered items.
*
* @returns {Object.<String,module:engine/model/schema~SchemaCompiledItemDefinition>}
*/
getDefinitions() {
if ( !this._compiledDefinitions ) {
this._compile();
}

return this._compiledDefinitions;
}

/**
* Returns a definition of the given item or `undefined` if item is not registered.
*
Expand Down Expand Up @@ -475,6 +470,70 @@ export default class Schema {
}, { priority: 'high' } );
}

/**
* Registers custom properties to a given attribute.
*
* It can be used to mark the attributes relation and handle them in a common way.
*
* // 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
* } );
*
* You can also use custom attributes:
*
* schema.setAttributeProperties( 'blockQuote', {
* customAttribute: 'value'
* } );
*
* Subsequent calls to the same attributes will add up the value:
*
* schema.setAttributeProperties( 'blockQuote', {
* one: 1
* } );
*
* schema.setAttributeProperties( 'blockQuote', {
* two: 2
* } );
*
* console.log( schema.getAttributeProperties( 'blockQuote' ) );
* // Logs: {one: 1, two: 2}
*
* @param {String} attributeName Name of the attribute to receive properties.
* @param {module:engine/model/schema~AttributeProperties} properties A 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 {module:engine/model/schema~AttributeProperties}
*/
getAttributeProperties( attributeName ) {
return this._attributeProperties[ attributeName ];
}

/**
* Returns all registered items.
*
* @returns {Object.<String,module:engine/model/schema~SchemaCompiledItemDefinition>}
*/
getDefinitions() {
if ( !this._compiledDefinitions ) {
this._compile();
}

return this._compiledDefinitions;
}

/**
* Returns the lowest {@link module:engine/model/schema~Schema#isLimit limit element} containing the entire
* selection/range/position or the root otherwise.
Expand Down Expand Up @@ -1285,6 +1344,15 @@ export class SchemaContext {
* @typedef {Object} module:engine/model/schema~SchemaContextItem
*/

/**
* A structure containing additional metadata describing the attribute.
*
* See {@link module:engine/model/schema~Schema#setAttributeProperties `Schema#setAttributeProperties()`} for usage examples.
*
* @typedef {Object} module:engine/model/schema~AttributeProperties
* @property {Boolean} [isFormatting] Indicates that the attribute should be considered as a visual formatting.
*/

function compileBaseItemRule( sourceItemRules, itemName ) {
const itemRule = {
name: itemName,
Expand Down
40 changes: 40 additions & 0 deletions tests/model/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,46 @@ describe( 'Schema', () => {
} );
} );

describe( 'setAttributeProperties()', () => {
beforeEach( () => {
schema.register( '$root' );
schema.register( 'paragraph', {
allowIn: '$root'
} );
schema.register( '$text', {
allowIn: 'paragraph'
} );
schema.extend( '$text', { allowAttributes: 'testAttribute' } );
} );

it( 'allows registering new properties', () => {
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.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' );
Expand Down

0 comments on commit bd25c72

Please sign in to comment.