// Generated by CoffeeScript 1.10.0 | ||
(function() { | ||
var BREAK_TAG_REGEX, CHECKPOINT_PREFIX, CHECKPOINT_SUFFIX, QUOTE_IDS, ensureTextNodeBetweenChildElements, findMicrosoftSplitter, removeNodes; | ||
var BREAK_TAG_REGEX, CHECKPOINT_PREFIX, CHECKPOINT_SUFFIX, QUOTE_IDS, elementIsAllContent, ensureTextNodeBetweenChildElements, findMicrosoftSplitter, findParentDiv, hasTagName, isTextNodeWrappedInSpan, removeNodes; | ||
@@ -15,3 +15,3 @@ CHECKPOINT_PREFIX = '#!%!'; | ||
var emailBodyElement, emailDocument, head, htmlElement; | ||
emailDocument = dom.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null); | ||
emailDocument = dom.implementation.createHTMLDocument(); | ||
htmlElement = emailDocument.getElementsByTagName('html')[0]; | ||
@@ -24,3 +24,5 @@ htmlElement.innerHTML = msgBody.trim(); | ||
head = emailDocument.getElementsByTagName('head')[0]; | ||
emailDocument.documentElement.removeChild(head); | ||
if (head) { | ||
emailDocument.documentElement.removeChild(head); | ||
} | ||
return emailDocument; | ||
@@ -36,3 +38,3 @@ }; | ||
if (htmlNode.nodeType === 1) { | ||
if (htmlNode.tagName !== 'body') { | ||
if (!hasTagName(htmlNode, 'body')) { | ||
htmlNode.innerHTML = " " + htmlNode.innerHTML + " "; | ||
@@ -61,3 +63,8 @@ } | ||
if (htmlNode.nodeType === 1) { | ||
childTagInQuotation = false; | ||
quotationChildren = []; | ||
if (!hasTagName(htmlNode, 'body')) { | ||
htmlNode.innerHTML = " " + htmlNode.innerHTML + " "; | ||
} | ||
ensureTextNodeBetweenChildElements(htmlNode); | ||
ref = htmlNode.childNodes; | ||
@@ -139,3 +146,3 @@ for (i = 0, len = ref.length; i < len; i++) { | ||
exports.cutFromBlock = function(emailDocument) { | ||
var afterSplitter, fromBlock, lastBlock, ref, splitterElement, textNode, xpathQuery, xpathResult; | ||
var afterSplitter, fromBlock, lastBlock, parentDiv, ref, splitterElement, textNode, xpathQuery, xpathResult; | ||
xpathQuery = "//*[starts-with(normalize-space(.), 'From:')]|//*[starts-with(normalize-space(.), 'Date:')]"; | ||
@@ -147,34 +154,55 @@ xpathResult = emailDocument.evaluate(xpathQuery, emailDocument, null, 5, null); | ||
if (lastBlock != null) { | ||
while ((lastBlock != null) && (lastBlock.parentElement != null)) { | ||
if (lastBlock.tagName === 'div') { | ||
lastBlock.parentElement.removeChild(lastBlock); | ||
return true; | ||
} else { | ||
lastBlock = lastBlock.parentElement; | ||
} | ||
parentDiv = findParentDiv(lastBlock); | ||
if ((parentDiv != null) && !elementIsAllContent(parentDiv)) { | ||
parentDiv.parentElement.removeChild(parentDiv); | ||
return true; | ||
} | ||
} else { | ||
xpathQuery = "//text()[starts-with(normalize-space(.), 'From:')]|//text()[starts-with(normalize-space(.), 'Date:')]"; | ||
xpathResult = emailDocument.evaluate(xpathQuery, emailDocument, null, 9, null); | ||
textNode = xpathResult.singleNodeValue; | ||
if (textNode == null) { | ||
return false; | ||
} | ||
xpathQuery = "//text()[starts-with(normalize-space(.), 'From:')]|//text()[starts-with(normalize-space(.), 'Date:')]"; | ||
xpathResult = emailDocument.evaluate(xpathQuery, emailDocument, null, 9, null); | ||
textNode = xpathResult.singleNodeValue; | ||
if (textNode == null) { | ||
return false; | ||
} | ||
if (isTextNodeWrappedInSpan(textNode)) { | ||
return false; | ||
} | ||
splitterElement = textNode.previousSibling; | ||
if (splitterElement != null) { | ||
if ((ref = splitterElement.parentElement) != null) { | ||
ref.removeChild(splitterElement); | ||
} | ||
splitterElement = textNode.previousSibling; | ||
if (splitterElement != null) { | ||
if ((ref = splitterElement.parentElement) != null) { | ||
ref.removeChild(splitterElement); | ||
} | ||
} | ||
} | ||
afterSplitter = textNode.nextSibling; | ||
while (afterSplitter != null) { | ||
afterSplitter.parentNode.removeChild(afterSplitter); | ||
afterSplitter = textNode.nextSibling; | ||
while (afterSplitter != null) { | ||
afterSplitter.parentNode.removeChild(afterSplitter); | ||
afterSplitter = textNode.nextSibling; | ||
} | ||
textNode.parentNode.removeChild(textNode); | ||
return true; | ||
}; | ||
findParentDiv = function(element) { | ||
while ((element != null) && (element.parentElement != null)) { | ||
if (hasTagName(element, 'div')) { | ||
return element; | ||
} else { | ||
element = element.parentElement; | ||
} | ||
textNode.parentNode.removeChild(textNode); | ||
return true; | ||
} | ||
return false; | ||
return null; | ||
}; | ||
elementIsAllContent = function(element) { | ||
var maybeBody; | ||
maybeBody = element.parentElement; | ||
return (maybeBody != null) && hasTagName(maybeBody, 'body') && maybeBody.childNodes.length === 1; | ||
}; | ||
isTextNodeWrappedInSpan = function(textNode) { | ||
var parentElement; | ||
parentElement = textNode.parentElement; | ||
return (parentElement != null) && hasTagName(parentElement, 'span') && parentElement.childNodes.length === 1; | ||
}; | ||
BREAK_TAG_REGEX = new RegExp('<br\\s*[/]?>', 'gi'); | ||
@@ -198,3 +226,3 @@ | ||
splitterElement = splitterResult[0]; | ||
if ((splitterElement.parentElement != null) && splitterElement === splitterElement.parentElement.chilren[0]) { | ||
if ((splitterElement.parentElement != null) && splitterElement === splitterElement.parentElement.children[0]) { | ||
splitterElement = splitterElement.parentElement; | ||
@@ -228,2 +256,7 @@ } | ||
currentNode = element.childNodes[0]; | ||
if (!currentNode) { | ||
newTextNode = dom.createTextNode(' '); | ||
element.appendChild(newTextNode); | ||
return; | ||
} | ||
results = []; | ||
@@ -240,2 +273,6 @@ while (currentNode.nextSibling) { | ||
hasTagName = function(element, tagName) { | ||
return element.tagName.toLowerCase() === tagName; | ||
}; | ||
}).call(this); |
{ | ||
"name": "planer", | ||
"version": "1.0.2", | ||
"version": "1.0.4", | ||
"description": "Remove reply quotations from emails", | ||
@@ -5,0 +5,0 @@ "main": "lib/planer.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
76874
25.94%14
7.69%495
7.14%