commit 5ee40f6601415c61a9ed61a7ee283640905524f5
parent f635d82ea654817d023bd2ba13724dfdd81b2f77
Author: Aurimas Vinckevicius <aurimas.dev@gmail.com>
Date: Mon, 6 Oct 2014 21:38:14 -0500
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
Diffstat:
2 files changed, 25 insertions(+), 5 deletions(-)
diff --git 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("<!DOCTYPE html><html></html>",
- "text/html");
- node = domDocument.createElement("div");
+ node = Zotero.Utilities.Internal.getDOMDocument().createElement("div");
}
node.innerHTML = str;
@@ -441,6 +437,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 <a href=""> links in plain text
*
* Ignore URLs preceded by '>', just in case there are already links
diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js
@@ -346,6 +346,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("<!DOCTYPE html><html></html>", "text/html");
+ },
+
+
+ /**
* A generator that yields promises that delay for the given intervals
*
* @param {Array<Integer>} intervals An array of intervals in milliseconds