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 #1111 from ckeditor/t/1110
Browse files Browse the repository at this point in the history
Fix: `Schema.checkAttributeInSelection` should use element's existing attributes when querying schema. Closes #1110.
  • Loading branch information
scofalik authored Aug 29, 2017
2 parents 6502bbb + d3ed54b commit 25ef1a8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/model/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/model/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,13 +492,18 @@ 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' } );
schema.allow( { name: 'p', attributes: 'bold', inside: '$root' } );

// 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', () => {
Expand Down Expand Up @@ -544,6 +549,16 @@ describe( 'Schema', () => {
setData( doc, '<p>foo[<img /><img />]bar</p>' );
expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
} );

it( 'should return true when checking element with required attribute', () => {
setData( doc, '[<figure name="figure"></figure>]' );
expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
} );

it( 'should return true when checking element when attribute is already present', () => {
setData( doc, '[<figure name="figure" title="title"></figure>]' );
expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
} );
} );
} );

Expand Down

0 comments on commit 25ef1a8

Please sign in to comment.