From ad0d6765d75842b1bd0b19c627381aae96deb3b5 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 11 Feb 2016 02:54:52 -0500 Subject: [PATCH] Fix Zotero.Attachments.linkFromDocument() --- chrome/content/zotero/xpcom/attachments.js | 37 ++++++++++------------ test/tests/attachmentsTest.js | 26 +++++++++++++++ 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index 52a630de8..012170d9c 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -510,28 +510,23 @@ Zotero.Attachments = new function(){ var title = document.title; // TODO: don't use Mozilla-generated title for images, etc. var contentType = document.contentType; - var item = yield Zotero.DB.executeTransaction(function* () { - return _addToDB({ - url: url, - title: title, - linkMode: this.LINK_MODE_LINKED_URL, - contentType: contentType, - charset: document.characterSet, - parentItemID: parentItemID, - collections: collections - }); - }.bind(this)); + var item = yield _addToDB({ + url, + title, + linkMode: this.LINK_MODE_LINKED_URL, + contentType, + charset: document.characterSet, + parentItemID, + collections + }); - // Run the indexer asynchronously - setTimeout(function () { - if (Zotero.Fulltext.isCachedMIMEType(contentType)) { - // No file, so no point running the PDF indexer - //Zotero.Fulltext.indexItems([itemID]); - } - else if (Zotero.MIME.isTextType(document.contentType)) { - Zotero.Fulltext.indexDocument(document, item.id); - } - }, 50); + if (Zotero.Fulltext.isCachedMIMEType(contentType)) { + // No file, so no point running the PDF indexer + //Zotero.Fulltext.indexItems([itemID]); + } + else if (Zotero.MIME.isTextType(document.contentType)) { + yield Zotero.Fulltext.indexDocument(document, item.id); + } return item; }); diff --git a/test/tests/attachmentsTest.js b/test/tests/attachmentsTest.js index c20cb2d79..63aa726e5 100644 --- a/test/tests/attachmentsTest.js +++ b/test/tests/attachmentsTest.js @@ -112,6 +112,32 @@ describe("Zotero.Attachments", function() { }) }) + describe("#linkFromDocument", function () { + it("should add a link attachment for the current webpage", function* () { + var item = yield createDataObject('item'); + + var uri = OS.Path.join(getTestDataDirectory().path, "snapshot", "index.html"); + var deferred = Zotero.Promise.defer(); + win.addEventListener('pageshow', () => deferred.resolve()); + win.loadURI(uri); + yield deferred.promise; + + var file = getTestDataDirectory(); + file.append('test.png'); + var attachment = yield Zotero.Attachments.linkFromDocument({ + document: win.content.document, + parentItemID: item.id + }); + + assert.equal(attachment.getField('url'), "file://" + uri); + + // Check indexing + var matches = yield Zotero.Fulltext.findTextInItems([attachment.id], 'works'); + assert.lengthOf(matches, 1); + assert.propertyVal(matches[0], 'id', attachment.id); + }) + }) + describe("#getTotalFileSize", function () { it("should return the size for a single-file attachment", function* () { var file = getTestDataDirectory();