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
This commit is contained in:
Aurimas Vinckevicius 2014-10-06 21:38:14 -05:00
parent f635d82ea6
commit 5ee40f6601
2 changed files with 25 additions and 5 deletions

View file

@ -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;
@ -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 <a href=""> links in plain text
*

View file

@ -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("<!DOCTYPE html><html></html>", "text/html");
},
/**
* A generator that yields promises that delay for the given intervals
*