Fix Zotero.Attachments.linkFromDocument()

This commit is contained in:
Dan Stillman 2016-02-11 02:54:52 -05:00
parent e137a05201
commit ad0d6765d7
2 changed files with 42 additions and 21 deletions

View File

@ -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;
});

View File

@ -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();