From 4bbfa212bc991ba1a22b1453daf7f75734de035d Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Fri, 15 Oct 2010 16:13:45 -0400 Subject: [PATCH] Somewhat better Y and :yank copy behavior that should work as expected on OS-X. --- common/content/buffer.js | 5 +---- common/content/commandline.js | 10 ++++------ common/modules/highlight.jsm | 2 +- common/modules/services.jsm | 21 +++++++++++--------- common/modules/util.jsm | 37 +++++++++++++++++++++-------------- 5 files changed, 40 insertions(+), 35 deletions(-) diff --git a/common/content/buffer.js b/common/content/buffer.js index 0f9f72e8..8c450fce 100644 --- a/common/content/buffer.js +++ b/common/content/buffer.js @@ -481,10 +481,7 @@ const Buffer = Module("buffer", { controller.setCaretEnabled(caretmode); return String.match(selection, /\w*/)[0]; } - if (util.computedStyle(range.startContainer).whiteSpace == "pre" - && util.computedStyle(range.endContainer).whiteSpace == "pre") - return String(range); - return String(selection); + return util.domToString(range); }, /** diff --git a/common/content/commandline.js b/common/content/commandline.js index cff4a08a..fd9f9e74 100644 --- a/common/content/commandline.js +++ b/common/content/commandline.js @@ -583,9 +583,6 @@ const CommandLine = Module("commandline", { this._lastMowOutput =
{str}
; let output = util.xmlToDom(this._lastMowOutput, doc); - if (!silent) - dactyl.triggerObserver("echoMultiline", str, highlightGroup, output); - // FIXME: need to make sure an open MOW is closed when commands // that don't generate output are executed if (this.widgets.mowContainer.collapsed) @@ -593,6 +590,9 @@ const CommandLine = Module("commandline", { doc.body.appendChild(output); + if (!silent) + dactyl.triggerObserver("echoMultiline", str, highlightGroup, output); + commandline.updateOutputHeight(true); if (options["more"] && win.scrollMaxY > 0) { @@ -1162,11 +1162,9 @@ const CommandLine = Module("commandline", { dactyl.registerObserver("echoLine", observe, true); dactyl.registerObserver("echoMultiline", observe, true); function observe(str, highlight, dom) { - if (!observe.done) - buffer.push(dom ? util.domToString(dom) : str) + buffer.push(dom ? util.domToString(dom) : str) } dactyl.trapErrors.apply(dactyl, [fn, self].concat(Array.slice(arguments, 2))); - observe.done = true; return buffer.join("\n"); } }, { diff --git a/common/modules/highlight.jsm b/common/modules/highlight.jsm index 6202bdb9..d93f4e5b 100644 --- a/common/modules/highlight.jsm +++ b/common/modules/highlight.jsm @@ -32,7 +32,7 @@ Highlight.defaultValue("baseClass", function () /^(\w*)/.exec(this.class)[0]); Highlight.defaultValue("selector", function () highlight.selector(this.class)); Highlight.defaultValue("sites", function () this.base ? this.base.sites - : ["chrome://dactyl/*", "dactyl:*", "file://*", "resource://gre-resources/hiddenWindow.html"].concat( + : ["chrome://dactyl/*", "dactyl:*", "file://*"].concat( highlight.styleableChrome)); Highlight.defaultValue("style", function () styles.addSheet(true, "highlight:" + this.class, this.sites, this.css, this.agent, true)); diff --git a/common/modules/services.jsm b/common/modules/services.jsm index 032ffa32..f7000727 100644 --- a/common/modules/services.jsm +++ b/common/modules/services.jsm @@ -56,15 +56,18 @@ const Services = Module("Services", { this.add("windowWatcher", "@mozilla.org/embedcomp/window-watcher;1", Ci.nsIWindowWatcher); - this.addClass("docshell", "@mozilla.org/docshell;1", [Ci.nsIBaseWindow, Ci.nsIWebNavigation, - Ci.nsIWebPageDescriptor, Ci.nsIWebProgress]); - this.addClass("file", "@mozilla.org/file/local;1", Ci.nsILocalFile); - this.addClass("file:", "@mozilla.org/network/protocol;1?name=file", Ci.nsIFileProtocolHandler); - this.addClass("find", "@mozilla.org/embedcomp/rangefind;1", Ci.nsIFind); - this.addClass("process", "@mozilla.org/process/util;1", Ci.nsIProcess); - this.addClass("timer", "@mozilla.org/timer;1", Ci.nsITimer); - this.addClass("xmlhttp", "@mozilla.org/xmlextras/xmlhttprequest;1", Ci.nsIXMLHttpRequest); - this.addClass("zipWriter", "@mozilla.org/zipwriter;1", Ci.nsIZipWriter); + this.addClass("docshell", "@mozilla.org/docshell;1", [Ci.nsIBaseWindow, Ci.nsIWebNavigation, + Ci.nsIWebPageDescriptor, Ci.nsIWebProgress]); + this.addClass("file", "@mozilla.org/file/local;1", Ci.nsILocalFile); + this.addClass("file:", "@mozilla.org/network/protocol;1?name=file", Ci.nsIFileProtocolHandler); + this.addClass("find", "@mozilla.org/embedcomp/rangefind;1", Ci.nsIFind); + this.addClass("htmlConverter","@mozilla.org/widget/htmlformatconverter;1", Ci.nsIFormatConverter); + this.addClass("htmlEncoder", "@mozilla.org/layout/htmlCopyEncoder;1", Ci.nsIDocumentEncoder); + this.addClass("process", "@mozilla.org/process/util;1", Ci.nsIProcess); + this.addClass("string", "@mozilla.org/supports-string;1", Ci.nsISupportsString); + this.addClass("timer", "@mozilla.org/timer;1", Ci.nsITimer); + this.addClass("xmlhttp", "@mozilla.org/xmlextras/xmlhttprequest;1", Ci.nsIXMLHttpRequest); + this.addClass("zipWriter", "@mozilla.org/zipwriter;1", Ci.nsIZipWriter); }, _create: function (classes, ifaces, meth) { diff --git a/common/modules/util.jsm b/common/modules/util.jsm index 3e3632fe..627c5356 100644 --- a/common/modules/util.jsm +++ b/common/modules/util.jsm @@ -43,10 +43,6 @@ const Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]) this.addObserver(this); this.overlays = {}; - - let doc = services.get("appShell").hiddenDOMWindow.document; - this._div = this.xmlToDom(
, doc); - doc.body.appendChild(this._div); }, // FIXME: Only works for Pentadactyl @@ -267,18 +263,29 @@ const Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]) pattern.replace(/\\(.)/, function (m0, m1) chars.indexOf(m1) >= 0 ? m1 : m0), domToString: function (node) { - this._div.appendChild(this._div.ownerDocument.importNode(node, true)); - if (/^pre(-wrap)?$/.test(this.computedStyle(this._div.lastChild).whiteSpace)) - var res = this._div.textContent; - else { - let sel = this._div.ownerDocument.defaultView.getSelection(); - sel.removeAllRanges(); - sel.selectAllChildren(this._div); - var res = sel.toString(); + if (node instanceof Ci.nsISelection && node.isCollapsed) + return ""; + + if (node instanceof Ci.nsIDOMNode) { + let range = node.ownerDocument.createRange(); + range.selectNode(node); + node = range; } - while (this._div.firstChild) - this._div.removeChild(this._div.firstChild); - return res; + let doc = (node.getRangeAt ? node.getRangeAt(0) : node).startContainer.ownerDocument; + + let encoder = services.create("htmlEncoder"); + encoder.init(doc, "text/unicode", encoder.OutputRaw|encoder.OutputPreformatted); + if (node instanceof Ci.nsISelection) + encoder.setSelection(node); + else if (node instanceof Ci.nsIDOMRange) + encoder.setRange(node); + + let str = services.create("string"); + str.data = encoder.encodeToString(); + + let [result, length] = [{}, {}]; + services.create("htmlConverter").convert("text/html", str, str.data.length*2, "text/unicode", result, length); + return result.value.QueryInterface(Ci.nsISupportsString).data; }, /**