From 242581a270daf602697dac7b74d8eced35a408b6 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 3 Jul 2015 22:06:12 -0400 Subject: [PATCH] Fix Quick Copy in tab mode, but for real --- chrome/content/zotero/xpcom/quickCopy.js | 14 ++++++++---- test/tests/quickCopyTest.js | 28 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 test/tests/quickCopyTest.js diff --git a/chrome/content/zotero/xpcom/quickCopy.js b/chrome/content/zotero/xpcom/quickCopy.js index 15b588fc8..a87768a1a 100644 --- a/chrome/content/zotero/xpcom/quickCopy.js +++ b/chrome/content/zotero/xpcom/quickCopy.js @@ -104,8 +104,9 @@ Zotero.QuickCopy = new function() { var ioService = Components.classes["@mozilla.org/network/io-service;1"] .getService(Components.interfaces.nsIIOService); + var nsIURI; try { - var nsIURI = ioService.newURI(url, null, null); + nsIURI = ioService.newURI(url, null, null); // Accessing some properties may throw for URIs that do not support those // parts. E.g. hostPort throws NS_ERROR_FAILURE for about:blank var urlHostPort = nsIURI.hostPort; @@ -113,8 +114,8 @@ Zotero.QuickCopy = new function() { } catch (e) {} - // Skip about:, chrome:, etc. - if (!urlHostPort) { + // Skip non-HTTP URLs + if (!/^https?$/.test(nsIURI.scheme)) { return quickCopyPref; } @@ -122,7 +123,12 @@ Zotero.QuickCopy = new function() { var sql = "SELECT key AS domainPath, value AS format FROM settings " + "WHERE setting='quickCopySite' AND (key LIKE ? OR key LIKE ?)"; - var urlDomain = urlHostPort.match(/[^\.]+\.[^\.]+$/); + var urlDomain = urlHostPort.match(/(?:[^\.]+\.)?[^\.]+$/); + // Hopefully can't happen, but until we're sure + if (!urlDomain) { + Zotero.logError("Quick Copy host '" + urlHostPort + "' not matched"); + return quickCopyPref; + } var rows = Zotero.DB.query(sql, ['%' + urlDomain[0] + '%', '/%']); for (let i = 0; i < rows.length; i++) { let row = rows[i]; diff --git a/test/tests/quickCopyTest.js b/test/tests/quickCopyTest.js new file mode 100644 index 000000000..5abf79f51 --- /dev/null +++ b/test/tests/quickCopyTest.js @@ -0,0 +1,28 @@ +describe("Zotero.QuickCopy", function() { + var quickCopyPref = Zotero.Prefs.get("export.quickCopy.setting"); + quickCopyPref = JSON.stringify(Zotero.QuickCopy.unserializeSetting(quickCopyPref)); + + // TODO: These should set site-specific prefs and test the actual response against it, + // but that will need to wait for 5.0. For now, just make sure they don't fail. + describe("#getFormatFromURL()", function () { + it("should handle a domain", function () { + assert.deepEqual(Zotero.QuickCopy.getFormatFromURL('http://foo.com/'), quickCopyPref); + }) + + it("should handle a domain and path", function () { + assert.deepEqual(Zotero.QuickCopy.getFormatFromURL('http://foo.com/bar'), quickCopyPref); + }) + + it("should handle a local host", function () { + assert.deepEqual(Zotero.QuickCopy.getFormatFromURL('http://foo/'), quickCopyPref); + }) + + it("should handle an about: URL", function () { + assert.deepEqual(Zotero.QuickCopy.getFormatFromURL('about:blank'), quickCopyPref); + }) + + it("should handle a chrome URL", function () { + assert.deepEqual(Zotero.QuickCopy.getFormatFromURL('chrome://zotero/content/tab.xul'), quickCopyPref); + }) + }) +})