Skip to content

Commit

Permalink
fix: error when removing 2nd list item (close #241) (#243)
Browse files Browse the repository at this point in the history
- it produce a arbitrary nesting list we can not process
- unwrap arbitrary nesting list
  • Loading branch information
kyuwoo-choi authored and seonim-ryu committed Jan 2, 2020
1 parent d6db563 commit fc56d47
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
21 changes: 19 additions & 2 deletions apps/core/src/js/wwListManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,35 @@ class WwListManager {
let nestedList = wrapperDiv.querySelector(NESTED_LIST_QUERY);
while (nestedList !== null) {
let prevLI = nestedList.previousElementSibling;
while (prevLI.tagName !== 'LI') {
while (prevLI && prevLI.tagName !== 'LI') {
prevLI = prevLI.previousElementSibling;
}

prevLI.appendChild(nestedList);
if (prevLI) {
prevLI.appendChild(nestedList);
} else {
this._unwrap(nestedList);
}

nestedList = wrapperDiv.querySelector(NESTED_LIST_QUERY);
}

return wrapperDiv.innerHTML;
}

/**
* unwrap nesting list
* @param {Node} nestedList - nested list to unwrap
* @private
*/
_unwrap(nestedList) {
const fragment = document.createDocumentFragment();
while (nestedList.firstChild) {
fragment.appendChild(nestedList.firstChild);
}
nestedList.parentNode.replaceChild(fragment, nestedList);
}

/**
* Return lines in selection
* @param {Node} start Start element
Expand Down
32 changes: 28 additions & 4 deletions apps/core/test/unit/wwListManager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ describe('WwListManager', () => {
'</ul>',
'<ol>',
'<li><div>me too!</div></li>',
'</ol>'].join(''));
'</ol>'
].join(''));

expect(wwe.get$Body().find('ul').length).toEqual(1);
expect(wwe.get$Body().find('ol').length).toEqual(1);
Expand Down Expand Up @@ -114,7 +115,8 @@ describe('WwListManager', () => {
'<li><div>t5</div></li>',
'</ul>',
'</li>',
'</ul>'].join(''));
'</ul>'
].join(''));
mgr._removeBranchListAll();

expect(wwe.get$Body().find('ul').length).toEqual(3);
Expand All @@ -136,7 +138,8 @@ describe('WwListManager', () => {
'</li>',
'<li><div>t2</div></li>',
'</ul>',
'</div>'].join(''));
'</div>'
].join(''));

mgr._removeBranchListAll();

Expand All @@ -155,7 +158,8 @@ describe('WwListManager', () => {
'<li><div>t4<br></div></li>',
'</ul>',
'</li>',
'</ul>'].join(''));
'</ul>'
].join(''));
mgr._removeBranchListAll();

expect(wwe.get$Body().find('ul').length).toEqual(2);
Expand Down Expand Up @@ -218,6 +222,26 @@ describe('WwListManager', () => {
});
});

describe('_unwrap', () => {
it('should unwrap itself and preserve the children', () => {
const arbitraryNestingList = $(`
<ul id="first">
<ul id="second">
<li>arbitrary nesting list item</li>
<li>arbitrary nesting list item</li>
</ul>
</ul>`).get(0);

mgr._unwrap(arbitraryNestingList.querySelector('ul ul'));

// <ul id="first">
// <li>arbitrary nesting list item</li>
// <li>arbitrary nesting list item</li>
// </ul>
expect(arbitraryNestingList.querySelector('ul ul')).toBeFalsy();
});
});

describe('mergeList', () => {
it('should merge list to previous list', () => {
const list = $(`
Expand Down

0 comments on commit fc56d47

Please sign in to comment.