Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup after ordered/unordered list item removal #1437

Merged
merged 1 commit into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Cleanup after ordered/unordered list item removal
  • Loading branch information
alexkurowski committed Apr 17, 2018
commit 243fc971197835149cd0c5ae27eb576032eb83f1
27 changes: 23 additions & 4 deletions spec/content.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
prepareEvent, selectElementContents,
selectElementContentsAndFire,
placeCursorInsideElement,
isFirefox */
isIE, getEdgeVersion, isFirefox */

describe('Content TestCase', function () {
'use strict';
Expand Down Expand Up @@ -824,7 +824,14 @@ describe('Content TestCase', function () {

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>');

if (isIE() || getEdgeVersion() > 0) {
// IE and Edge wraps elements in div
expect(this.el.innerHTML).toBe('<div>lorem</div><ul><li>ipsum</li><li>dolor</li></ul>');
} else {
// Other browsers should wrap them in p
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 () {
Expand All @@ -844,7 +851,13 @@ describe('Content TestCase', function () {
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>');
if (isIE() || getEdgeVersion() > 0) {
// IE and Edge wraps elements in div
expect(this.el.innerHTML).toBe('<div>lorem</div><div>ipsum</div><ol><li>dolor</li></ol>');
} else {
// Other browsers should wrap them in p
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 () {
Expand All @@ -859,7 +872,13 @@ describe('Content TestCase', function () {

selectElementContentsAndFire(target);
fireEvent(toolbar.getToolbarElement().querySelector('[data-action="insertunorderedlist"]'), 'click');
expect(this.el.innerHTML).toBe('<p>lorem</p><p>ipsum</p><p>dolor</p>');
if (isIE() || getEdgeVersion() > 0) {
// IE and Edge wraps elements in div
expect(this.el.innerHTML).toBe('<div>lorem</div><div>ipsum</div><div>dolor</div>');
} else {
// Other browsers should wrap them in p
expect(this.el.innerHTML).toBe('<p>lorem</p><p>ipsum</p><p>dolor</p>');
}
});
});
});
34 changes: 31 additions & 3 deletions src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,28 @@
return false;
},

findFirstTextNodeInSelection: function (selection) {
if (selection.anchorNode.nodeType === 3) {
return selection.anchorNode;
}

var node = selection.anchorNode.firstChild;

while (node) {
if (selection.containsNode(node, true)) {
if (node.nodeType === 3) {
return node;
} else {
node = node.firstChild;
}
} else {
node = node.nextSibling;
}
}

return null;
},

cleanListDOM: function (ownerDocument, element) {
if (element.nodeName.toLowerCase() !== 'li') {
if (this.isIE || this.isEdge) {
Expand All @@ -686,23 +708,29 @@
startOffset = oldRange.startOffset,
endContainer = oldRange.endContainer,
endOffset = oldRange.endOffset,
node, newNode, nextNode;
node, newNode, nextNode, moveEndOffset;

if (element.nodeName.toLowerCase() === 'span') {
// Chrome & Safari unwraps removed li elements into a span
node = element;
moveEndOffset = false;
} else {
// FF leaves them as text nodes
node = startContainer;
node = this.findFirstTextNodeInSelection(selection);
moveEndOffset = startContainer.nodeType !== 3;
}

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

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

if (moveEndOffset) {
endOffset--;
}
}

nextNode = node.nextSibling;
Expand Down