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

Fix: Make sure that schema#getAttributeProperties() always return an object.

NOTE: This fixes a behavior that has not been officially released yet, so it might be reasonable to skip this in the changelog.
Marek Lewandowski 6 лет назад
Родитель
Сommit
5178e6b11d

+ 1 - 1
packages/ckeditor5-engine/src/model/schema.js

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

+ 31 - 19
packages/ckeditor5-engine/tests/model/schema.js

@@ -96,7 +96,7 @@ describe( 'Schema', () => {
 		} );
 	} );
 
-	describe( 'setAttributeProperties()', () => {
+	describe( 'attribute properties', () => {
 		beforeEach( () => {
 			schema.register( '$root' );
 			schema.register( 'paragraph', {
@@ -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( {} );
 			} );
 		} );
 	} );