Skip to content

Instantly share code, notes, and snippets.

@caisui
Last active December 16, 2015 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caisui/5404569 to your computer and use it in GitHub Desktop.
Save caisui/5404569 to your computer and use it in GitHub Desktop.
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