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 #39 from ckeditor/t/38
Browse files Browse the repository at this point in the history
Feature: Integrated the command with `Schema#limits`. Closes #38.
  • Loading branch information
Reinmar authored Feb 26, 2017
2 parents 8841be8 + a0d81ef commit 36dac9b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/entercommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class EnterCommand extends Command {
const batch = doc.batch();

doc.enqueueChanges( () => {
enterBlock( this.editor.data, batch, doc.selection );
enterBlock( this.editor.data, batch, doc.selection, doc.schema );

this.fire( 'afterExecute', { batch } );
} );
Expand All @@ -36,12 +36,18 @@ export default class EnterCommand extends Command {
// @param {engine.controller.DataController} dataController
// @param {module:engine/model/batch~Batch} batch A batch to which the deltas will be added.
// @param {module:engine/model/selection~Selection} selection Selection on which the action should be performed.
function enterBlock( dataController, batch, selection ) {
// @param {module:engine/model/schema~Schema} schema
function enterBlock( dataController, batch, selection, schema ) {
const isSelectionEmpty = selection.isCollapsed;
const range = selection.getFirstRange();
const startElement = range.start.parent;
const endElement = range.end.parent;

// Do nothing if selection starts or ends inside `limit` elements.
if ( schema.limits.has( startElement.name ) || schema.limits.has( endElement.name ) ) {
return;
}

// Don't touch the root.
if ( startElement.root == startElement ) {
if ( !isSelectionEmpty ) {
Expand Down
34 changes: 34 additions & 0 deletions tests/entercommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ describe( 'EnterCommand', () => {
schema.registerItem( 'img', '$inline' );
schema.registerItem( 'p', '$block' );
schema.registerItem( 'h', '$block' );
schema.registerItem( 'inlineLimit' );
schema.registerItem( 'blockLimit' );

schema.allow( { name: 'inlineLimit', inside: 'p' } );
schema.allow( { name: '$text', inside: 'inlineLimit' } );
schema.allow( { name: '$text', inside: '$root' } );
schema.allow( { name: 'blockLimit', inside: '$root' } );
schema.allow( { name: 'p', inside: 'blockLimit' } );

schema.limits.add( 'inlineLimit' );
schema.limits.add( 'blockLimit' );
} );
} );

Expand Down Expand Up @@ -107,6 +117,30 @@ describe( 'EnterCommand', () => {
'<p>[]</p>'
);

test(
'should not break inline limit elements - collapsed',
'<p><inlineLimit>foo[]bar</inlineLimit></p>',
'<p><inlineLimit>foo[]bar</inlineLimit></p>'
);

test(
'should not break inline limit elements',
'<p><inlineLimit>foo[bar]baz</inlineLimit></p>',
'<p><inlineLimit>foo[bar]baz</inlineLimit></p>'
);

test(
'should not break inline limit elements - selection partially inside',
'<p><inlineLimit>ba[r</inlineLimit></p><p>f]oo</p>',
'<p><inlineLimit>ba[r</inlineLimit></p><p>f]oo</p>'
);

test(
'should break paragraph in blockLimit',
'<blockLimit><p>foo[]bar</p></blockLimit>',
'<blockLimit><p>foo</p><p>[]bar</p></blockLimit>'
);

it( 'leaves one empty element after two were fully selected (backward)', () => {
setData( doc, '<p>[abc</p><p>def]</p>' );
// @TODO: Add option for setting selection direction to model utils.
Expand Down

0 comments on commit 36dac9b

Please sign in to comment.