Skip to content

Commit

Permalink
Merge 85111fe into 3fcd1b2
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkurowski committed Apr 13, 2018
2 parents 3fcd1b2 + 85111fe commit cfd8320
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 10 deletions.
52 changes: 52 additions & 0 deletions spec/content.spec.js
Expand Up @@ -810,4 +810,56 @@ describe('Content TestCase', function () {
expect(para.querySelectorAll('div').length).toBe(0, 'Some <br> elements were replaced with <div> elements within the <p>');
});
});

describe('when list element is unlisted', function () {
it('should fix markup when one list element is unlisted', function () {
this.el.innerHTML = '<ul><li>lorem</li><li>ipsum</li><li>dolor</li></ul>';
var editor = this.newMediumEditor('.editor', {
toolbar: {
buttons: ['unorderedlist']
}
}),
target = editor.elements[0].querySelector('li'),
toolbar = editor.getExtensionByName('toolbar');

selectElementContentsAndFire(target);
fireEvent(toolbar.getToolbarElement().querySelector('[data-action="insertunorderedlist"]'), 'click');
expect(this.el.innerHTML).toBe('<p>lorem</p><ul><li>ipsum</li><li>dolor</li></ul>');
});

it('should fix markup when miltiple list elements are unlisted', function () {
this.el.innerHTML = '<ol><li>lorem</li><li>ipsum</li><li>dolor</li></ol>';
var editor = this.newMediumEditor('.editor', {
toolbar: {
buttons: ['orderedlist']
}
}),
toolbar = editor.getExtensionByName('toolbar'),
selection = document.getSelection(),
range = document.createRange();

range.setStart(this.el.querySelectorAll('li')[0].firstChild, 0);
range.setEnd(this.el.querySelectorAll('li')[1].firstChild, 5);
selection.removeAllRanges();
selection.addRange(range);

fireEvent(toolbar.getToolbarElement().querySelector('[data-action="insertorderedlist"]'), 'click');
expect(this.el.innerHTML).toBe('<p>lorem</p><p>ipsum</p><ol><li>dolor</li></ol>');
});

it('should fix markup when all list elements are unlisted', function () {
this.el.innerHTML = '<ul><li>lorem</li><li>ipsum</li><li>dolor</li></ul>';
var editor = this.newMediumEditor('.editor', {
toolbar: {
buttons: ['unorderedlist']
}
}),
target = editor.elements[0].querySelector('ul'),
toolbar = editor.getExtensionByName('toolbar');

selectElementContentsAndFire(target);
fireEvent(toolbar.getToolbarElement().querySelector('[data-action="insertunorderedlist"]'), 'click');
expect(this.el.innerHTML).toBe('<p>lorem</p><p>ipsum</p><p>dolor</p>');
});
});
});
2 changes: 1 addition & 1 deletion src/js/core.js
Expand Up @@ -939,7 +939,7 @@

// do some DOM clean-up for known browser issues after the action
if (action === 'insertunorderedlist' || action === 'insertorderedlist') {
MediumEditor.util.cleanListDOM(this.options.ownerDocument, this.getSelectedParentElement());
MediumEditor.util.cleanListDOM(this.options.contentWindow, this.options.ownerDocument, this.getSelectedParentElement());
}

this.checkSelection();
Expand Down
57 changes: 48 additions & 9 deletions src/js/util.js
Expand Up @@ -673,19 +673,58 @@
return false;
},

cleanListDOM: function (ownerDocument, element) {
cleanListDOM: function (contentWindow, ownerDocument, element) {
if (element.nodeName.toLowerCase() !== 'li') {
return;
}
var selection = contentWindow.getSelection(),
newRange = ownerDocument.createRange(),
oldRange = selection.getRangeAt(0),
startContainer = oldRange.startContainer,
startOffset = oldRange.startOffset,
endContainer = oldRange.endContainer,
endOffset = oldRange.endOffset,
node, newNode, nextNode;

if (element.nodeName.toLowerCase() === 'span') {
// Chrome unwraps removed li elements into a span
node = element;
} else {
// FF leaves them as text nodes
node = startContainer;
}

while (node) {
if (node.nodeName.toLowerCase() !== 'span' && node.nodeName.toLowerCase() !== '#text') {
break;
}

if (node.nextSibling && node.nextSibling.nodeName.toLowerCase() === 'br') {
node.nextSibling.remove();
}

nextNode = node.nextSibling;

var list = element.parentElement;
newNode = ownerDocument.createElement('p');
node.parentNode.replaceChild(newNode, node);
newNode.appendChild(node);

if (list.parentElement.nodeName.toLowerCase() === 'p') { // yes we need to clean up
Util.unwrap(list.parentElement, ownerDocument);
node = nextNode;
}

// move cursor at the end of the text inside the list
// for some unknown reason, the cursor is moved to end of the "visual" line
MediumEditor.selection.moveCursor(ownerDocument, element.firstChild, element.firstChild.textContent.length);
// Restore selection
newRange.setStart(startContainer, startOffset);
newRange.setEnd(endContainer, endOffset);
selection.removeAllRanges();
selection.addRange(newRange);
} else {
var list = element.parentElement;

if (list.parentElement.nodeName.toLowerCase() === 'p') { // yes we need to clean up
Util.unwrap(list.parentElement, ownerDocument);

// move cursor at the end of the text inside the list
// for some unknown reason, the cursor is moved to end of the "visual" line
MediumEditor.selection.moveCursor(ownerDocument, element.firstChild, element.firstChild.textContent.length);
}
}
},

Expand Down

0 comments on commit cfd8320

Please sign in to comment.