From d3ed54b4c64c62222591603007541ddb06db33e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Kup=C5=9B?= Date: Mon, 28 Aug 2017 17:08:59 +0200 Subject: [PATCH] Schema.checkAttributeInSelection adds existing element's attributes to the schema query. --- src/model/schema.js | 6 +++++- tests/model/schema/schema.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/model/schema.js b/src/model/schema.js index 26f1ba45c..f18eae5e2 100644 --- a/src/model/schema.js +++ b/src/model/schema.js @@ -332,7 +332,11 @@ export default class Schema { // If returned item does not have name property, it is a TextFragment. const name = value.item.name || '$text'; - if ( this.check( { name, inside: value.previousPosition, attributes: attribute } ) ) { + // Attribute should be checked together with existing attributes. + // See https://github.com/ckeditor/ckeditor5-engine/issues/1110. + const attributes = Array.from( value.item.getAttributeKeys() ).concat( attribute ); + + if ( this.check( { name, inside: value.previousPosition, attributes } ) ) { // If we found a node that is allowed to have the attribute, return true. return true; } diff --git a/tests/model/schema/schema.js b/tests/model/schema/schema.js index b11c2a633..3a3a26a0d 100644 --- a/tests/model/schema/schema.js +++ b/tests/model/schema/schema.js @@ -492,6 +492,7 @@ describe( 'Schema', () => { schema.registerItem( 'p', '$block' ); schema.registerItem( 'h1', '$block' ); schema.registerItem( 'img', '$inline' ); + schema.registerItem( 'figure' ); // Bold text is allowed only in P. schema.allow( { name: '$text', attributes: 'bold', inside: 'p' } ); @@ -499,6 +500,10 @@ describe( 'Schema', () => { // Disallow bold on image. schema.disallow( { name: 'img', attributes: 'bold', inside: '$root' } ); + + // Figure must have name attribute and optional title attribute. + schema.requireAttributes( 'figure', [ 'name' ] ); + schema.allow( { name: 'figure', attributes: [ 'title', 'name' ], inside: '$root' } ); } ); describe( 'when selection is collapsed', () => { @@ -544,6 +549,16 @@ describe( 'Schema', () => { setData( doc, '

foo[]bar

' ); expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false; } ); + + it( 'should return true when checking element with required attribute', () => { + setData( doc, '[
]' ); + expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true; + } ); + + it( 'should return true when checking element when attribute is already present', () => { + setData( doc, '[
]' ); + expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true; + } ); } ); } );