Skip to content

Commit

Permalink
fixed undo; removal extended to block elements; test refactoring; sim…
Browse files Browse the repository at this point in the history
…plebox replaced by image2
  • Loading branch information
bunglegrind authored and Dumluregn committed Feb 22, 2021
1 parent aa78cb9 commit 1152d32
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 242 deletions.
7 changes: 6 additions & 1 deletion core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,10 @@
return text === '';
}

function isEmptyBlock( block ) {
return block.isBlockBoundary() && isEmptyElement( block );
}

return function( evt ) {
var keystroke = evt.data.getKeystroke();

Expand All @@ -616,8 +620,9 @@
if ( next && next.type == CKEDITOR.NODE_ELEMENT && next.getAttribute( 'contenteditable' ) == 'false' ) {
//added if case to allow removal of empty paragraphs, see incident #1572
startElement = sel.getStartElement();
if ( startElement.getName() === 'p' && isDeleteAction( keystroke ) && isEmptyElement( startElement ) ) {
if ( isEmptyBlock( startElement ) && isDeleteAction( keystroke ) ) {
startElement.remove();
editor.fire( 'saveSnapshot' );//saves an undo restore point
}

editor.getSelection().fake( next );
Expand Down
Binary file added tests/plugins/widget/_assets/bar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
165 changes: 165 additions & 0 deletions tests/plugins/widget/deleteemptyblockbetweenwidgets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/* bender-tags: editor */
/* bender-ckeditor-plugins: wysiwygarea,toolbar,forms,widget,codesnippet,image2,undo,basicstyles,entities */

'use strict';
( function() {
var backspace = 8,
deleteKey = 46,
leftArrow = 37,
rightArrow = 39,

image2Setup = (
'<figure class="image">' +
'<img alt="alternative text" width="100" height="50" src="_assets/bar.png" />' +
'<figcaption>I&#39;m a description</figcaption>' +
'</figure>'
),

codeSnippetSetup = (
'<pre><code class="language-javascript">//Hello another world!</code></pre>'
);

bender.test( {

// (#1572)
'test press delete to remove an empty block between two widgets': testFactory(
{
input: image2Setup + createBlock( '&nbsp' ) + codeSnippetSetup,
result: image2Setup + codeSnippetSetup,
keyCode: deleteKey,
config: {
allowedContent: true
},
assertion: assertBlockDeleted
}
),

// (#1572)
'test press backspace to remove an empty block between two widgets': testFactory(
{
input: image2Setup + createBlock( '&nbsp' ) + codeSnippetSetup,
result: image2Setup + codeSnippetSetup,
keyCode: backspace,
config: {
allowedContent: true
},
assertion: assertBlockDeleted
}
),

// (#1572)
'test press right arrow must not remove a block between two widgets': testFactory(
{
input: image2Setup + createBlock( '&nbsp' ) + codeSnippetSetup,
keyCode: rightArrow,
config: {
allowedContent: true
},
assertion: assertNothingHasChanged
}
),

// (#1572)
'test press left arrow must not remove a block between two widgets': testFactory(
{
input: image2Setup + createBlock( '&nbsp' ) + codeSnippetSetup,
keyCode: leftArrow,
config: {
allowedContent: true
},
assertion: assertNothingHasChanged
}
),

// (#1572)
'test press delete must not remove a not empty block between two widgets': testFactory(
{
input: image2Setup + createBlock( 'Not empty' ) + codeSnippetSetup,
keyCode: deleteKey,
config: {
allowedContent: true
},
assertion: assertBlockNotDeleted
}
),

// (#1572)
'test press backspace must not remove a not empty block between two widgets': testFactory(
{
input: image2Setup + createBlock( 'Not empty' ) + codeSnippetSetup,
keyCode: backspace,
config: {
allowedContent: true
},
assertion: assertBlockNotDeleted
}
)
} );

function testFactory( params ) {
var input = params.input,
result = params.result || input,
keyCode = params.keyCode,
config = params.config || {},
assertion = params.assertion;

return function() {
bender.editorBot.create( {
name: 'editor' + new Date().getTime(),
startupData: input,
config: config
}, function( bot ) {
var editor = bot.editor;

selectBlock( editor );
editor.editable().fire( 'keydown', new CKEDITOR.dom.event( { keyCode: keyCode } ) );

assertion( editor, result );
} );

};

}

function selectBlock( editor ) {
var range = editor.createRange();

range.moveToPosition( editor.document.getById( 'block' ), CKEDITOR.POSITION_AFTER_START );
editor.getSelection().selectRanges( [ range ] );
}

function createBlock( fillerText, blockTag ) {
var blockElements = [ 'p', 'h1', 'div', 'header', 'nav', 'section' ],
block;

blockTag = blockTag ? blockTag : blockElements[ Math.floor( Math.random() * blockElements.length ) ];

block = new CKEDITOR.dom.element( blockTag );
block.setAttribute( 'id', 'block' );
block.$.innerHTML = fillerText;
return block.getOuterHtml();
}

function assertBlockDeleted( editor, result ) {
var snapshots = editor.undoManager.snapshots;

assert.areSame( result, editor.getData(), 'The empty block between the two widgets should be removed.' );

assert.areSame( 2, snapshots.length, 'Two undo snapshots should be created at this point.' );
assert.isFalse( snapshots[ 0 ].equalsContent( snapshots[ 1 ] ), 'The snapshots should be different.' );
}

function assertNothingHasChanged( editor, result ) {

assert.areSame( result, editor.getData(), 'The empty block between the two widgets should not be removed.' );
}

function assertBlockNotDeleted( editor ) {
var block = editor.document.getById( 'block' );

assert.isNotUndefined( block, 'The block between the two widgets should not be removed.' );

assert.isNotNull( block, 'The block between the two widgets should not be removed.' );
}

} )();
193 changes: 0 additions & 193 deletions tests/plugins/widget/deleteparagraphsclosetowidgets.js

This file was deleted.

Loading

0 comments on commit 1152d32

Please sign in to comment.