From 5ee40f6601415c61a9ed61a7ee283640905524f5 Mon Sep 17 00:00:00 2001 From: Aurimas Vinckevicius Date: Mon, 6 Oct 2014 21:38:14 -0500 Subject: [PATCH] Add ZU.dom2text and Zotero.Utilities.Internal.getDOMDocument * getDomDocument: returns a detached DOMDocument object * dom2text (TODO): Currently just returns Node.textContent, but is intended to return Zotero-formatted string based on text formatting in the DOM and the Zotero.Item field that the text is meant for --- chrome/content/zotero/xpcom/utilities.js | 20 ++++++++++++++----- .../zotero/xpcom/utilities_internal.js | 10 ++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js index 990a7a7a25..a3b1571974 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 887874c362..18f0ae1f15 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 *