Skip to content

Commit

Permalink
added fix for backspace/delete from empty blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
spocke committed Aug 2, 2016
1 parent 8c841a2 commit 68d6c78
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
42 changes: 34 additions & 8 deletions js/tinymce/classes/SelectionOverrides.js
Expand Up @@ -428,25 +428,51 @@ define("tinymce/SelectionOverrides", [
return null;
}

function isTextBlock(node) {
var textBlocks = editor.schema.getTextBlockElements();
return node.nodeName in textBlocks;
}

function isEmpty(elm) {
return editor.dom.isEmpty(elm);
}

function mergeTextBlocks(direction, fromCaretPosition, toCaretPosition) {
var dom = editor.dom, fromBlock, toBlock, node, textBlocks;
var dom = editor.dom, fromBlock, toBlock, node, ceTarget;

fromBlock = dom.getParent(fromCaretPosition.getNode(), dom.isBlock);
toBlock = dom.getParent(toCaretPosition.getNode(), dom.isBlock);

if (direction === -1) {
if (isAfterContentEditableFalse(toCaretPosition) && isBlock(toCaretPosition.getNode(true))) {
ceTarget = toCaretPosition.getNode(true);
if (isAfterContentEditableFalse(toCaretPosition) && isBlock(ceTarget)) {
if (isTextBlock(fromBlock)) {
if (isEmpty(fromBlock)) {
dom.remove(fromBlock);
}

return CaretPosition.after(ceTarget).toRange();
}

return deleteContentEditableNode(toCaretPosition.getNode(true));
}
} else {
if (isBeforeContentEditableFalse(fromCaretPosition) && isBlock(fromCaretPosition.getNode())) {
ceTarget = fromCaretPosition.getNode();
if (isBeforeContentEditableFalse(fromCaretPosition) && isBlock(ceTarget)) {
if (isTextBlock(toBlock)) {
if (isEmpty(toBlock)) {
dom.remove(toBlock);
}

return CaretPosition.before(ceTarget).toRange();
}

return deleteContentEditableNode(fromCaretPosition.getNode());
}
}

textBlocks = editor.schema.getTextBlockElements();
fromBlock = dom.getParent(fromCaretPosition.getNode(), dom.isBlock);
toBlock = dom.getParent(toCaretPosition.getNode(), dom.isBlock);

// Verify that both blocks are text blocks
if (fromBlock === toBlock || !textBlocks[fromBlock.nodeName] || !textBlocks[toBlock.nodeName]) {
if (fromBlock === toBlock || !isTextBlock(fromBlock) || !isTextBlock(toBlock)) {
return null;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/tinymce/SelectionOverrides.js
Expand Up @@ -276,6 +276,24 @@ ModuleLoader.require([
equal(editor.selection.getRng().startContainer.nextSibling.nodeName, 'SPAN');
});

test('backspace from empty block to after cE=false', function() {
editor.getBody().innerHTML = '<p contenteditable="false">1</p><p><br></p>';
Utils.setSelection('p:nth-child(2)', 0);

backspace();
equal(editor.getContent(), '<p contenteditable="false">1</p>');
assertCaretInCaretBlockContainer();
});

test('delete from empty block to before cE=false', function() {
editor.getBody().innerHTML = '<p><br></p><p contenteditable="false">2</p>';
Utils.setSelection('p:nth-child(1)', 0);

forwardDelete();
equal(editor.getContent(), '<p contenteditable="false">2</p>');
assertCaretInCaretBlockContainer();
});

test('exit pre block (up)', exitPreTest(upArrow, 0, '<p>\u00a0</p><pre>abc</pre>'));
test('exit pre block (left)', exitPreTest(leftArrow, 0, '<p>\u00a0</p><pre>abc</pre>'));
test('exit pre block (down)', exitPreTest(downArrow, 3, '<pre>abc</pre><p>\u00a0</p>'));
Expand Down

0 comments on commit 68d6c78

Please sign in to comment.