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

Commit

Permalink
Merge pull request #1718 from ckeditor/t/1717
Browse files Browse the repository at this point in the history
Fix: Make sure that `schema#getAttributeProperties()` always return an object. Closes #1717.
  • Loading branch information
jodator authored Apr 3, 2019
2 parents c48f5a4 + bd3d37d commit b3f5da3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/model/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ export default class Schema {
* @param {module:engine/model/schema~AttributeProperties} properties A dictionary of properties.
*/
setAttributeProperties( attributeName, properties ) {
this._attributeProperties[ attributeName ] = Object.assign( this.getAttributeProperties( attributeName ) || {}, properties );
this._attributeProperties[ attributeName ] = Object.assign( this.getAttributeProperties( attributeName ), properties );
}

/**
Expand All @@ -532,7 +532,7 @@ export default class Schema {
* @returns {module:engine/model/schema~AttributeProperties}
*/
getAttributeProperties( attributeName ) {
return this._attributeProperties[ attributeName ];
return this._attributeProperties[ attributeName ] || {};
}

/**
Expand Down
50 changes: 31 additions & 19 deletions tests/model/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe( 'Schema', () => {
} );
} );

describe( 'setAttributeProperties()', () => {
describe( 'attribute properties', () => {
beforeEach( () => {
schema.register( '$root' );
schema.register( 'paragraph', {
Expand All @@ -105,33 +105,45 @@ describe( 'Schema', () => {
schema.register( '$text', {
allowIn: 'paragraph'
} );
schema.extend( '$text', { allowAttributes: 'testAttribute' } );
schema.extend( '$text', { allowAttributes: [ 'testAttribute', 'noPropertiesAttribute' ] } );
} );

it( 'allows registering new properties', () => {
schema.setAttributeProperties( 'testAttribute', {
foo: 'bar',
baz: 'bom'
} );
describe( 'setAttributeProperties()', () => {
it( 'allows registering new properties', () => {
schema.setAttributeProperties( 'testAttribute', {
foo: 'bar',
baz: 'bom'
} );

expect( schema.getAttributeProperties( 'testAttribute' ) ).to.deep.equal( {
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'
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'
} );
} );
} );

schema.setAttributeProperties( 'testAttribute', {
second: 'bar'
describe( 'getAttributeProperties()', () => {
it( 'it returns a proper value if the attribute has no properties', () => {
expect( schema.getAttributeProperties( 'noPropertiesAttribute' ) ).to.deep.equal( {} );
} );

expect( schema.getAttributeProperties( 'testAttribute' ) ).to.deep.equal( {
first: 'foo',
second: 'bar'
it( 'it returns a proper value for unknown attribute', () => {
expect( schema.getAttributeProperties( 'unregistered-attribute' ) ).to.deep.equal( {} );
} );
} );
} );
Expand Down

0 comments on commit b3f5da3

Please sign in to comment.