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

Wrap lists + remove nested divs #78

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions source/Clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ var stylesRewriters = {
INS: replaceWithTag( 'U' ),
STRIKE: replaceWithTag( 'S' ),
P: replaceWithTag('DIV'),
DIV: function ( node, parent ) {
if (node.querySelectorAll('div').length) {
return removeNested('div', node)
}

return node
},
FONT: function ( node, parent ) {
var face = node.face,
size = node.size,
Expand Down Expand Up @@ -318,7 +325,14 @@ var cleanTree = function cleanTree ( node, preserveWS ) {
l += childLength - 1;
node.replaceChild( empty( child ), child );
continue;
} else if (nodeName === 'LI' && !hasAncestor(child, ['UL', 'OL'])) {
const ulNode = createElement(doc, 'UL')
child.parentNode.insertBefore(ulNode, child)
const slurpedNodes = slurpNodes(ulNode, 'LI')
l -= slurpedNodes.length
continue;
}

if ( childLength ) {
cleanTree( child, preserveWS || ( nodeName === 'PRE' ) );
}
Expand Down
37 changes: 37 additions & 0 deletions source/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,43 @@ function fixContainer ( container, root ) {
return container;
}

function hasAncestor(node, parents=[]) {
if (!node.parentNode) {
return false
}

if (parents.includes(node.parentNode.tagName)) {
return true
}

return hasAncestor(node.parentNode, parents)
}

function slurpNodes(node, tagToSlurp, slurpedNodes) {
slurpedNodes = slurpedNodes || []

const next = node.nextSibling

if (next && next.tagName === 'LI') {
const detachedLi = detach(next)
node.appendChild(detachedLi)
return slurpNodes(node, tagToSlurp, slurpedNodes.concat([detachedLi]))
}

return slurpedNodes
}

function removeNested(tag, node) {
const nesteds = node.querySelectorAll(tag)

_.each(nesteds, function (nested) {
nested.outerHTML = nested.innerHTML
})

return node
}


function split ( node, offset, stopNode, root ) {
var nodeType = node.nodeType,
parent, clone, next;
Expand Down