diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js index 990a7a7a2..a3b157197 100644 --- a/chrome/content/zotero/xpcom/utilities.js +++ b/chrome/content/zotero/xpcom/utilities.js @@ -411,11 +411,7 @@ Zotero.Utilities = { // Create a node and use the textContent property to do unescaping where // possible, because this approach preserves line endings in the HTML if(node === undefined) { - var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"] - .createInstance(Components.interfaces.nsIDOMParser); - var domDocument = parser.parseFromString("", - "text/html"); - node = domDocument.createElement("div"); + node = Zotero.Utilities.Internal.getDOMDocument().createElement("div"); } node.innerHTML = str; @@ -440,6 +436,20 @@ Zotero.Utilities = { }; }, + /** + * Converts text inside a DOM object to plain text preserving text formatting + * appropriate for given field + * + * @param {DOMNode} rootNode Node containing all the text that needs to be extracted + * @param {String} targetField Zotero item field that the text is meant for + * + * @return {String} Zotero formatted string + */ + "dom2text": function(rootNode, targetField) { + // TODO: actually do this + return Zotero.Utilities.trimInternal(rootNode.textContent); + }, + /** * Wrap URLs and DOIs in links in plain text * diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index 887874c36..18f0ae1f1 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -345,6 +345,16 @@ Zotero.Utilities.Internal = { }, + /** + * Returns a DOMDocument object not attached to any window + */ + "getDOMDocument": function() { + return Components.classes["@mozilla.org/xmlextras/domparser;1"] + .createInstance(Components.interfaces.nsIDOMParser) + .parseFromString("", "text/html"); + }, + + /** * A generator that yields promises that delay for the given intervals *