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

Commit 07b37af

Browse files
authored
Merge pull request #25 from ckeditor/t/24
Fix: `ParagraphCommand` should check whether it can be applied to the selection. Closes #24.
2 parents 5007cdc + 3f8f6c4 commit 07b37af

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/paragraphcommand.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,11 @@ export default class ParagraphCommand extends Command {
2929
* @inheritDoc
3030
*/
3131
refresh() {
32-
const block = first( this.editor.document.selection.getSelectedBlocks() );
32+
const document = this.editor.document;
33+
const block = first( document.selection.getSelectedBlocks() );
3334

3435
this.value = !!block && block.is( 'paragraph' );
35-
36-
this.isEnabled = !!block && this.editor.document.schema.check( {
37-
name: 'paragraph',
38-
inside: Position.createBefore( block )
39-
} );
36+
this.isEnabled = !!block && checkCanBecomeParagraph( block, document.schema );
4037
}
4138

4239
/**
@@ -58,10 +55,23 @@ export default class ParagraphCommand extends Command {
5855
const blocks = ( options.selection || document.selection ).getSelectedBlocks();
5956

6057
for ( const block of blocks ) {
61-
if ( !block.is( 'paragraph' ) ) {
58+
if ( !block.is( 'paragraph' ) && checkCanBecomeParagraph( block, document.schema ) ) {
6259
batch.rename( block, 'paragraph' );
6360
}
6461
}
6562
} );
6663
}
6764
}
65+
66+
// Checks whether the given block can be replaced by a paragraph.
67+
//
68+
// @private
69+
// @param {module:engine/model/element~Element} block A block to be tested.
70+
// @param {module:engine/model/schema~Schema} schema The schema of the document.
71+
// @returns {Boolean}
72+
function checkCanBecomeParagraph( block, schema ) {
73+
return schema.check( {
74+
name: 'paragraph',
75+
inside: Position.createBefore( block )
76+
} );
77+
}

tests/paragraphcommand.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,31 @@ describe( 'ParagraphCommand', () => {
9999
expect( command.value ).to.be.true;
100100
} );
101101

102+
// https://github.com/ckeditor/ckeditor5-paragraph/issues/24
103+
it( 'should not rename blocks which cannot become paragraphs', () => {
104+
document.schema.registerItem( 'restricted' );
105+
document.schema.allow( { name: 'restricted', inside: '$root' } );
106+
document.schema.disallow( { name: 'paragraph', inside: 'restricted' } );
107+
108+
document.schema.registerItem( 'fooBlock', '$block' );
109+
document.schema.allow( { name: 'fooBlock', inside: 'restricted' } );
110+
111+
setData(
112+
document,
113+
'<heading1>a[bc</heading1>' +
114+
'<restricted><fooBlock></fooBlock></restricted>' +
115+
'<heading1>de]f</heading1>'
116+
);
117+
118+
command.execute();
119+
120+
expect( getData( document ) ).to.equal(
121+
'<paragraph>a[bc</paragraph>' +
122+
'<restricted><fooBlock></fooBlock></restricted>' +
123+
'<paragraph>de]f</paragraph>'
124+
);
125+
} );
126+
102127
it( 'should not rename blocks which already are pargraphs', () => {
103128
const batch = editor.document.batch();
104129

0 commit comments

Comments
 (0)