Last active
December 16, 2015 08:18
-
-
Save caisui/5404569 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/common/content/util.js b/common/content/util.js | |
--- a/common/content/util.js | |
+++ b/common/content/util.js | |
@@ -797,11 +797,46 @@ | |
} | |
return dom.childNodes.length === 1 ? dom.childNodes[0] : dom; | |
}, | |
- domToStr: function domToStr(node) { | |
- var enc=Cc["@mozilla.org/layout/documentEncoder;1?type=text/plain"].getService(Ci.nsIDocumentEncoder); | |
- enc.init(node.ownerDocument, "text/html", 0); | |
- enc.setNode(node); | |
- return enc.encodeToString(); | |
+ /** | |
+ * encoding dom | |
+ * | |
+ * @param {Node|Range|Selection} node | |
+ * @param {String} type example "text/plain", "text/html", "text/xml" | |
+ * @param {Number} flags nsIDocumentEncoder.OutputXXXX | |
+ * @returns {String} | |
+ */ | |
+ domToStr: let (Encoder = Components.Constructor("@mozilla.org/layout/documentEncoder;1?type=text/plain", "nsIDocumentEncoder", "init")) | |
+ function domToStr(node, type, flags) { | |
+ var doc, method; | |
+ | |
+ if (node instanceof Document) { | |
+ doc = node; | |
+ node = null; | |
+ method = "setNode"; | |
+ } else if (node instanceof Node) { | |
+ doc = node.ownerDocument; | |
+ method = "setNode"; | |
+ } else if (node instanceof Range) { | |
+ doc = node.startContainer; | |
+ if (doc.ownerDocument) { | |
+ doc = doc.ownerDocument; | |
+ } | |
+ method = "setRange"; | |
+ } else if (node instanceof Selection) { | |
+ // can not found document | |
+ if (node.rangeCount === 0) { | |
+ return ""; | |
+ } | |
+ doc = node.getRangeAt(0).startContainer; | |
+ if (doc.ownerDocument) { | |
+ doc = doc.ownerDocument; | |
+ } | |
+ method = "setSelection"; | |
+ } | |
+ | |
+ var encoder = new Encoder(doc, type || "text/html", flags || 0); | |
+ encoder[method](node); | |
+ return encoder.encodeToString(); | |
}, | |
}, { | |
// TODO: Why don't we just push all util.BuiltinType up into modules? --djk |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment