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

Commit 25ef1a8

Browse files
authored
Merge pull request #1111 from ckeditor/t/1110
Fix: `Schema.checkAttributeInSelection` should use element's existing attributes when querying schema. Closes #1110.
2 parents 6502bbb + d3ed54b commit 25ef1a8

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/model/schema.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,11 @@ export default class Schema {
332332
// If returned item does not have name property, it is a TextFragment.
333333
const name = value.item.name || '$text';
334334

335-
if ( this.check( { name, inside: value.previousPosition, attributes: attribute } ) ) {
335+
// Attribute should be checked together with existing attributes.
336+
// See https://github.com/ckeditor/ckeditor5-engine/issues/1110.
337+
const attributes = Array.from( value.item.getAttributeKeys() ).concat( attribute );
338+
339+
if ( this.check( { name, inside: value.previousPosition, attributes } ) ) {
336340
// If we found a node that is allowed to have the attribute, return true.
337341
return true;
338342
}

tests/model/schema/schema.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,13 +492,18 @@ describe( 'Schema', () => {
492492
schema.registerItem( 'p', '$block' );
493493
schema.registerItem( 'h1', '$block' );
494494
schema.registerItem( 'img', '$inline' );
495+
schema.registerItem( 'figure' );
495496

496497
// Bold text is allowed only in P.
497498
schema.allow( { name: '$text', attributes: 'bold', inside: 'p' } );
498499
schema.allow( { name: 'p', attributes: 'bold', inside: '$root' } );
499500

500501
// Disallow bold on image.
501502
schema.disallow( { name: 'img', attributes: 'bold', inside: '$root' } );
503+
504+
// Figure must have name attribute and optional title attribute.
505+
schema.requireAttributes( 'figure', [ 'name' ] );
506+
schema.allow( { name: 'figure', attributes: [ 'title', 'name' ], inside: '$root' } );
502507
} );
503508

504509
describe( 'when selection is collapsed', () => {
@@ -544,6 +549,16 @@ describe( 'Schema', () => {
544549
setData( doc, '<p>foo[<img /><img />]bar</p>' );
545550
expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
546551
} );
552+
553+
it( 'should return true when checking element with required attribute', () => {
554+
setData( doc, '[<figure name="figure"></figure>]' );
555+
expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
556+
} );
557+
558+
it( 'should return true when checking element when attribute is already present', () => {
559+
setData( doc, '[<figure name="figure" title="title"></figure>]' );
560+
expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
561+
} );
547562
} );
548563
} );
549564

0 commit comments

Comments
 (0)