From 8e575ae55d9eb12d117a339238939666aeb69c30 Mon Sep 17 00:00:00 2001 From: Milo Mordaunt Date: Wed, 15 Nov 2017 19:21:51 -0500 Subject: [PATCH 1/3] wrap lists + remove nested divs --- source/Clean.js | 14 ++++++++++++++ source/Node.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/source/Clean.js b/source/Clean.js index 7433d85..2a8b0ff 100644 --- a/source/Clean.js +++ b/source/Clean.js @@ -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, @@ -318,7 +325,14 @@ var cleanTree = function cleanTree ( node, preserveWS ) { l += childLength - 1; node.replaceChild( empty( child ), child ); continue; + } else if (nodeName === 'LI' && unparentedLi(child)) { + const ulNode = createElement(doc, 'UL') + child.parentNode.insertBefore(ulNode, child) + const slurpCount = slurpLis(ulNode) + l -= slurpCount + continue; } + if ( childLength ) { cleanTree( child, preserveWS || ( nodeName === 'PRE' ) ); } diff --git a/source/Node.js b/source/Node.js index 9fac0ba..a846b3d 100644 --- a/source/Node.js +++ b/source/Node.js @@ -441,6 +441,36 @@ function fixContainer ( container, root ) { return container; } +function unparentedLi(node) { + return !node.parentNode || + !['UL', 'OL'].includes(node.parentNode.tagName) +} + +function slurpLis(node, slurpCount) { + slurpCount = slurpCount || 0 + + const next = node.nextSibling + + if (next && next.tagName === 'LI') { + const detachedLi = detach(next) + node.appendChild(detachedLi) + return slurpLis(node, slurpCount + 1) + } + + return slurpCount +} + +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; From ea25d0a05781bd8b05e09aa8793118e39296095f Mon Sep 17 00:00:00 2001 From: Milo Mordaunt Date: Fri, 17 Nov 2017 10:48:55 -0500 Subject: [PATCH 2/3] Tidier codes --- source/Clean.js | 6 +++--- source/Node.js | 21 ++++++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/source/Clean.js b/source/Clean.js index 2a8b0ff..368fe31 100644 --- a/source/Clean.js +++ b/source/Clean.js @@ -325,11 +325,11 @@ var cleanTree = function cleanTree ( node, preserveWS ) { l += childLength - 1; node.replaceChild( empty( child ), child ); continue; - } else if (nodeName === 'LI' && unparentedLi(child)) { + } else if (nodeName === 'LI' && !isParentedBy(child, ['UL', 'OL'])) { const ulNode = createElement(doc, 'UL') child.parentNode.insertBefore(ulNode, child) - const slurpCount = slurpLis(ulNode) - l -= slurpCount + const slurpedNodes = slurpNodes(ulNode, 'LI') + l -= slurpedNodes.length continue; } diff --git a/source/Node.js b/source/Node.js index a846b3d..1b8233a 100644 --- a/source/Node.js +++ b/source/Node.js @@ -441,23 +441,30 @@ function fixContainer ( container, root ) { return container; } -function unparentedLi(node) { - return !node.parentNode || - !['UL', 'OL'].includes(node.parentNode.tagName) +function isParentedBy(node, parents=[]) { + if (!node.parentNode) { + return false + } + + if (parents.includes(node.parentNode.tagName)) { + return true + } + + return isParentedBy(node.parentNode, parents) } -function slurpLis(node, slurpCount) { - slurpCount = slurpCount || 0 +function slurpNodes(node, tagToSlurp, slurpedNodes) { + slurpedNodes = slurpedNodes || [] const next = node.nextSibling if (next && next.tagName === 'LI') { const detachedLi = detach(next) node.appendChild(detachedLi) - return slurpLis(node, slurpCount + 1) + return slurpNodes(node, tagToSlurp, slurpedNodes.concat([detachedLi])) } - return slurpCount + return slurpedNodes } function removeNested(tag, node) { From 806cc41e2097bf1dde8ab0eae2e590ee57c426e1 Mon Sep 17 00:00:00 2001 From: Milo Mordaunt Date: Fri, 17 Nov 2017 11:17:47 -0500 Subject: [PATCH 3/3] parent => ancestor --- source/Clean.js | 2 +- source/Node.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/Clean.js b/source/Clean.js index 368fe31..8b3c854 100644 --- a/source/Clean.js +++ b/source/Clean.js @@ -325,7 +325,7 @@ var cleanTree = function cleanTree ( node, preserveWS ) { l += childLength - 1; node.replaceChild( empty( child ), child ); continue; - } else if (nodeName === 'LI' && !isParentedBy(child, ['UL', 'OL'])) { + } else if (nodeName === 'LI' && !hasAncestor(child, ['UL', 'OL'])) { const ulNode = createElement(doc, 'UL') child.parentNode.insertBefore(ulNode, child) const slurpedNodes = slurpNodes(ulNode, 'LI') diff --git a/source/Node.js b/source/Node.js index 1b8233a..5caade4 100644 --- a/source/Node.js +++ b/source/Node.js @@ -441,7 +441,7 @@ function fixContainer ( container, root ) { return container; } -function isParentedBy(node, parents=[]) { +function hasAncestor(node, parents=[]) { if (!node.parentNode) { return false } @@ -450,7 +450,7 @@ function isParentedBy(node, parents=[]) { return true } - return isParentedBy(node.parentNode, parents) + return hasAncestor(node.parentNode, parents) } function slurpNodes(node, tagToSlurp, slurpedNodes) {