Skip to content

Commit

Permalink
Fix truncating pasted nested list
Browse files Browse the repository at this point in the history
Fixes #421
  • Loading branch information
jhchen committed Jul 23, 2015
1 parent 07b05a4 commit 44d2632
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/core/normalizer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Normalizer
normalizeLine: (lineNode) ->
lineNode = Normalizer.wrapInline(lineNode)
lineNode = Normalizer.handleBreaks(lineNode)
if lineNode.tagName == 'LI'
Normalizer.flattenList(lineNode)
lineNode = Normalizer.pullBlocks(lineNode)
lineNode = this.normalizeNode(lineNode)
Normalizer.unwrapText(lineNode)
Expand Down Expand Up @@ -127,6 +129,18 @@ class Normalizer
curNode = curNode.nextSibling
return lineNode

@flattenList: (listNode) ->
ref = listNode.nextSibling
innerItems = _.map(listNode.querySelectorAll('li'))
innerItems.forEach((item) ->
listNode.parentNode.insertBefore(item, ref)
ref = item.nextSibling
)
innerLists = _.map(listNode.querySelectorAll(Object.keys(dom.LIST_TAGS).join(',')))
innerLists.forEach((list) ->
dom(list).remove()
)

@stripComments: (html) ->
return html.replace(/<!--[\s\S]*?-->/g, '')

Expand Down
6 changes: 6 additions & 0 deletions test/unit/core/document.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ describe('Document', ->
'list':
initial: '<div><ul><li>line</li></ul></div>'
expected: '<ul><li>line</li></ul>'
'nested divs':
initial: '<div><div>One<div><div>Alpha<div><div>I</div></div></div></div></div></div>'
expected: '<div>One</div><div>Alpha</div><div>I</div>'
'nested list':
initial: '<ul><li>One<ul><li>Alpha<ul><li>I</li></ul></li></ul></li></ul>'
expected: '<ul><li>One</li><li>Alpha</li><li>I</li></ul>'

_.each(tests, (test, name) ->
it(name, ->
Expand Down
8 changes: 7 additions & 1 deletion test/unit/core/normalizer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,13 @@ describe('Normalizer', ->
initial: '<div></div>'
expected: '<div></div>'
'After block':
initial: '<div>One<div>Two</div>'
initial: '<div>One<div>Two</div></div>'
expected: '<div>One</div><div>Two</div>'
'Before inner block':
initial: '<div><div>One<div>Two</div></div></div>'
expected: '<div>One</div><div>Two</div>'
'After inner block':
initial: '<div><div><div>One</div>Two</div></div>'
expected: '<div>One</div><div>Two</div>'
'Middle block':
initial: '<div>One<div>Two</div>Three</div>'
Expand Down

0 comments on commit 44d2632

Please sign in to comment.