Skip to content

Commit

Permalink
Fix bug on enter Backspace between two UL elements should connect the…
Browse files Browse the repository at this point in the history
…se elements
  • Loading branch information
xdan committed Feb 5, 2018
1 parent 0d37d61 commit 23efb87
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 42 deletions.
54 changes: 28 additions & 26 deletions src/plugins/backspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,22 @@ export function backspace(editor: Jodit) {

if (range) {
const textNode: Node = range.startContainer.nodeType === Node.TEXT_NODE ? range.startContainer : range.startContainer.childNodes[range.startOffset];
const startOffsetInRange: number = range.startContainer.nodeType === Node.TEXT_NODE ? range.startOffset : 0;
let startOffset: number = startOffsetInRange;

if (textNode && textNode.nodeType === Node.TEXT_NODE && textNode.nodeValue) {
let value: string = textNode.nodeValue,
startOffset: number = range.startOffset,
increment: number = toLeft ? -1 : 1;

while (startOffset >= 0 && startOffset <= value.length && value[startOffset + increment] === consts.INVISIBLE_SPACE) {
startOffset += increment;
}

if (startOffset !== range.startOffset) {
const oldStart = range.startOffset;
if (startOffset !== startOffsetInRange) {
if (toLeft) {
value = value.substr(0, startOffset) + value.substr(oldStart);
value = value.substr(0, startOffset) + value.substr(startOffsetInRange);
} else {
value = value.substr(0, oldStart) + value.substr(startOffset);
value = value.substr(0, startOffsetInRange) + value.substr(startOffset);
}

textNode.nodeValue = value;
Expand All @@ -69,35 +69,37 @@ export function backspace(editor: Jodit) {
}
}

if (range.startOffset === 0 && toLeft && textNode) {
if (startOffset === 0 && toLeft && textNode) {
const prevBox: Node | false = Dom.prev(textNode, Dom.isBlock, editor.editor);

if (prevBox) {
editor.selection.setCursorIn(prevBox, false);
}

const container: HTMLElement | null = <HTMLElement | null>Dom.up(range.startContainer, Dom.isBlock, editor.editor);

if (container) {
const html: string = container.innerHTML.replace(consts.INVISIBLE_SPACE_REG_EXP, '');
if ((!html.length || html == '<br>') && !Dom.isCell(container, editor.editorWindow) && container.parentNode && container !== editor.editor) {
container.parentNode.removeChild(container);

// Ul near with UL
if (prevBox && prevBox.nodeName === 'LI') {
const UL: Node | false = Dom.closest(prevBox, 'Ul|OL', editor.editor);
if (UL) {
const nextBox: Node | null = UL.nextSibling;
if (nextBox && nextBox.nodeName === UL.nodeName) {
[].slice.call(nextBox.childNodes).forEach(function (node: HTMLLIElement) {
UL.appendChild(node);
});
nextBox.parentNode && nextBox.parentNode.removeChild(nextBox);
}
try {
const container: HTMLElement | null = <HTMLElement | null>Dom.up(range.startContainer, Dom.isBlock, editor.editor);

if (container) {
const html: string = container.innerHTML.replace(consts.INVISIBLE_SPACE_REG_EXP, '');
if ((!html.length || html == '<br>') && !Dom.isCell(container, editor.editorWindow) && container.parentNode && container !== editor.editor) {
container.parentNode.removeChild(container);

return false;
}
}
} finally {
// Ul near with UL
if (prevBox && prevBox.nodeName === 'LI') {
const UL: Node | false = Dom.closest(prevBox, 'Ul|OL', editor.editor);
if (UL) {
const nextBox: Node | null = UL.nextSibling;
if (nextBox && nextBox.nodeName === UL.nodeName) {
[].slice.call(nextBox.childNodes).forEach(function (node: HTMLLIElement) {
UL.appendChild(node);
});
nextBox.parentNode && nextBox.parentNode.removeChild(nextBox);
}
}

return false;
}
}
}
Expand Down
56 changes: 40 additions & 16 deletions test/tests/enterTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,54 @@ describe('Enter behavior Jodit Editor Tests', function() {
'</tbody></table>');
});
describe('Enter backspace in the middle of two UL elements', function () {
it('Should connect both UL in one element', function () {
var editor = new Jodit(appendTestArea())
editor.setEditorValue('<ul><li>Test</li><li></li><li>Some text</li></ul>');
describe('In the P element', function () {
it('Should connect both UL in one element', function () {
var editor = new Jodit(appendTestArea())
editor.setEditorValue('<ul><li>Test</li><li></li><li>Some text</li></ul>');

var sel = editor.editorWindow.getSelection(),
range = editor.editorDocument.createRange();
var sel = editor.editorWindow.getSelection(),
range = editor.editorDocument.createRange();

range.setStart(editor.editor.firstChild.childNodes[1], 0);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
range.setStart(editor.editor.firstChild.childNodes[1], 0);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);

simulateEvent('keydown', Jodit.KEY_ENTER, editor.editor);
simulateEvent('keydown', Jodit.KEY_ENTER, editor.editor);

expect(editor.getEditorValue()).to.be.equal('<ul><li>Test</li></ul><p></p><ul><li>Some text</li></ul>');
expect(editor.getEditorValue()).to.be.equal('<ul><li>Test</li></ul><p></p><ul><li>Some text</li></ul>');

simulateEvent('keydown', Jodit.KEY_BACKSPACE, editor.editor);
simulateEvent('keydown', Jodit.KEY_BACKSPACE, editor.editor);

expect(editor.getEditorValue()).to.be.equal('<ul><li>Test</li><li>Some text</li></ul>');
expect(editor.getEditorValue()).to.be.equal('<ul><li>Test</li><li>Some text</li></ul>');


editor.selection.insertNode(editor.editorDocument.createTextNode(' a '))
expect(editor.getEditorValue()).to.be.equal('<ul><li>Test a </li><li>Some text</li></ul>');
})
editor.selection.insertNode(editor.editorDocument.createTextNode(' a '))
expect(editor.getEditorValue()).to.be.equal('<ul><li>Test a </li><li>Some text</li></ul>');
})
});
describe('In the empty space', function () {
it('Should connect both UL in one element', function () {
var editor = new Jodit(appendTestArea())
editor.setEditorValue('<ul><li>Test</li></ul><ul><li>Some text</li></ul>');

var sel = editor.editorWindow.getSelection(),
range = editor.editorDocument.createRange();

range.setStartBefore(editor.editor.childNodes[1]);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);

simulateEvent('keydown', Jodit.KEY_BACKSPACE, editor.editor);

expect(editor.getEditorValue()).to.be.equal('<ul><li>Test</li><li>Some text</li></ul>');


editor.selection.insertNode(editor.editorDocument.createTextNode(' a '))
expect(editor.getEditorValue()).to.be.equal('<ul><li>Test a </li><li>Some text</li></ul>');
})
});
});
});
describe('Enter key', function () {
Expand Down

0 comments on commit 23efb87

Please sign in to comment.