Skip to content

Commit

Permalink
Fixes #121 list toggle - improve line break handling and reduce defer…
Browse files Browse the repository at this point in the history
…red function calls
  • Loading branch information
Oliver Pulges committed Mar 16, 2015
1 parent 994834e commit bc28e08
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/commands/insertList.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ wysihtml5.commands.insertList = (function(wysihtml5) {
// <ul><li>foo</li><li>bar</li></ul>
// becomes:
// foo<br>bar<br>
composer.selection.executeAndRestore(function() {
var otherLists = getListsInSelection(otherNodeName, composer);

composer.selection.executeAndRestoreRangy(function() {
otherLists = getListsInSelection(otherNodeName, composer);
if (otherLists.length) {
for (var l = otherLists.length; l--;) {
wysihtml5.dom.renameElement(otherLists[l], nodeName.toLowerCase());
Expand All @@ -81,7 +82,7 @@ wysihtml5.commands.insertList = (function(wysihtml5) {
// becomes:
// <ul><li>foo</li><li>bar</li></ul>
// Also rename other lists in selection
composer.selection.executeAndRestore(function() {
composer.selection.executeAndRestoreRangy(function() {
var renameLists = [el].concat(getListsInSelection(otherNodeName, composer));

// All selection inner lists get renamed too
Expand Down
8 changes: 6 additions & 2 deletions src/dom/resolve_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@
var doc = list.ownerDocument,
fragment = doc.createDocumentFragment(),
previousSibling = wysihtml5.dom.domNode(list).prev({ignoreBlankTexts: true}),
nextSibling = wysihtml5.dom.domNode(list).next({ignoreBlankTexts: true}),
firstChild,
lastChild,
isLastChild,
shouldAppendLineBreak,
paragraph,
listItem;
listItem,
lastListItem = list.lastElementChild || list.lastChild,
isLastItem;

if (useLineBreaks) {
// Insert line break if list is after a non-block element
Expand All @@ -57,10 +60,11 @@

while (listItem = (list.firstElementChild || list.firstChild)) {
lastChild = listItem.lastChild;
isLastItem = listItem === lastListItem;
while (firstChild = listItem.firstChild) {
isLastChild = firstChild === lastChild;
// This needs to be done before appending it to the fragment, as it otherwise will lose style information
shouldAppendLineBreak = isLastChild && !_isBlockElement(firstChild) && !_isLineBreak(firstChild);
shouldAppendLineBreak = (!isLastItem || (nextSibling && !_isBlockElement(nextSibling))) && isLastChild && !_isBlockElement(firstChild) && !_isLineBreak(firstChild);
fragment.appendChild(firstChild);
if (shouldAppendLineBreak) {
_appendLineBreak(fragment);
Expand Down
21 changes: 15 additions & 6 deletions test/dom/resolve_list_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,41 @@ module("wysihtml5.dom.resolveList", {
test("Basic tests (useLineBreaks = true)", function() {
this.equal(
this.resolveList("<ul><li>foo</li></ul>", true),
"foo<br>"
"foo",
"List with one li element resolved"
);

this.equal(
this.resolveList("<ul><li>foo</li></ul>Test", true),
"foo<br>Test",
"List with test after adds line break"
);

this.equal(
this.resolveList("<ul><li>foo</li><li>bar</li></ul>", true),
"foo<br>bar<br>"
"foo<br>bar",
"List with two li elements resolved"
);

this.equal(
this.resolveList("<ol><li>foo</li><li>bar</li></ol>", true),
"foo<br>bar<br>"
"foo<br>bar",
"Numbered list with two li elements resolved"
);

this.equal(
this.resolveList("<ol><li></li><li>bar</li></ol>", true),
"bar<br>"
"bar"
);

this.equal(
this.resolveList("<ol><li>foo<br></li><li>bar</li></ol>", true),
"foo<br>bar<br>"
"foo<br>bar"
);

this.equal(
this.resolveList("<ul><li><h1>foo</h1></li><li><div>bar</div></li><li>baz</li></ul>", true),
"<h1>foo</h1><div>bar</div>baz<br>"
"<h1>foo</h1><div>bar</div>baz"
);
});

Expand Down

0 comments on commit bc28e08

Please sign in to comment.