Fix hang on import that includes an HTML attachment

Closes #734, for the moment
This commit is contained in:
Dan Stillman 2016-03-22 00:40:59 -04:00
parent 42968949b6
commit 74cf2a3c22
5 changed files with 119 additions and 24 deletions

View file

@ -1342,15 +1342,11 @@ Zotero.Attachments = new function(){
browser.addEventListener("pageshow", onpageshow, false); browser.addEventListener("pageshow", onpageshow, false);
} }
else { else {
let callback = function(charset, args) { let callback = Zotero.Promise.coroutine(function* (charset, args) {
// ignore spurious about:blank loads // ignore spurious about:blank loads
if(browser.contentDocument.location.href == "about:blank") return; if(browser.contentDocument.location.href == "about:blank") return;
// Since the callback can be called during an import process that uses try {
// Zotero.wait(), wait until we're unlocked
Zotero.unlockPromise
.then(function () {
return Zotero.spawn(function* () {
if (charset) { if (charset) {
charset = Zotero.CharacterSets.toCanonical(charset); charset = Zotero.CharacterSets.toCanonical(charset);
if (charset) { if (charset) {
@ -1365,12 +1361,11 @@ Zotero.Attachments = new function(){
Zotero.Browser.deleteHiddenBrowser(browser); Zotero.Browser.deleteHiddenBrowser(browser);
deferred.resolve(); deferred.resolve();
}); }
}) catch (e) {
.catch(function (e) {
deferred.reject(e); deferred.reject(e);
}
}); });
};
Zotero.File.addCharsetListener(browser, callback, item.id); Zotero.File.addCharsetListener(browser, callback, item.id);
} }

View file

@ -200,6 +200,8 @@ function waitForItemEvent(event) {
* Wait for a single notifier event and return a promise for the data * Wait for a single notifier event and return a promise for the data
*/ */
function waitForNotifierEvent(event, type) { function waitForNotifierEvent(event, type) {
if (!event) throw new Error("event not provided");
var deferred = Zotero.Promise.defer(); var deferred = Zotero.Promise.defer();
var notifierID = Zotero.Notifier.registerObserver({notify:function(ev, type, ids, extraData) { var notifierID = Zotero.Notifier.registerObserver({notify:function(ev, type, ids, extraData) {
if(ev == event) { if(ev == event) {

View file

@ -93,7 +93,7 @@ describe("Zotero.Attachments", function() {
}); });
}) })
describe("#linkToFile()", function () { describe("#linkFromFile()", function () {
it("should link to a file in My Library", function* () { it("should link to a file in My Library", function* () {
var item = yield createDataObject('item'); var item = yield createDataObject('item');
@ -112,6 +112,45 @@ describe("Zotero.Attachments", function() {
}) })
}) })
describe("#importSnapshotFromFile()", function () {
it("should import an HTML file", function* () {
var item = yield createDataObject('item');
var file = getTestDataDirectory();
file.append('test.html');
var attachment = yield Zotero.Attachments.importSnapshotFromFile({
title: 'Snapshot',
url: 'http://example.com',
file,
parentItemID: item.id,
contentType: 'text/html',
charset: 'utf-8'
});
var matches = yield Zotero.Fulltext.findTextInItems([attachment.id], 'test');
assert.lengthOf(matches, 1);
assert.propertyVal(matches[0], 'id', attachment.id);
});
it("should detect charset for an HTML file", function* () {
var item = yield createDataObject('item');
var file = getTestDataDirectory();
file.append('test.html');
var attachment = yield Zotero.Attachments.importSnapshotFromFile({
title: 'Snapshot',
url: 'http://example.com',
file,
parentItemID: item.id,
contentType: 'text/html'
});
assert.equal(attachment.attachmentCharset, 'utf-8');
var matches = yield Zotero.Fulltext.findTextInItems([attachment.id], 'test');
assert.lengthOf(matches, 1);
assert.propertyVal(matches[0], 'id', attachment.id);
});
});
describe("#linkFromDocument", function () { describe("#linkFromDocument", function () {
it("should add a link attachment for the current webpage", function* () { it("should add a link attachment for the current webpage", function* () {
var item = yield createDataObject('item'); var item = yield createDataObject('item');

View file

@ -0,0 +1,26 @@
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:z="http://www.zotero.org/namespaces/export#"
xmlns:link="http://purl.org/rss/1.0/modules/link/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:bib="http://purl.org/net/biblio#"
xmlns:dcterms="http://purl.org/dc/terms/">
<bib:Book rdf:about="#item_1">
<z:itemType>book</z:itemType>
<link:link rdf:resource="#item_2"/>
<dc:title>Test</dc:title>
</bib:Book>
<z:Attachment rdf:about="#item_2">
<z:itemType>attachment</z:itemType>
<rdf:resource rdf:resource="files/2/test.html"/>
<dc:identifier>
<dcterms:URI>
<rdf:value>http://example.com/</rdf:value>
</dcterms:URI>
</dc:identifier>
<dcterms:dateSubmitted>2016-03-22 04:55:27</dcterms:dateSubmitted>
<dc:title>Test</dc:title>
<z:linkMode>1</z:linkMode>
<link:type>text/html</link:type>
</z:Attachment>
</rdf:RDF>

View file

@ -38,4 +38,37 @@ describe("Zotero_File_Interface", function() {
} }
assert.deepEqual(savedItems, trueItems, "saved items match inputs") assert.deepEqual(savedItems, trueItems, "saved items match inputs")
}); });
it('should import an item and snapshot from Zotero RDF', function* () {
var tmpDir = yield getTempDirectory();
var rdfFile = OS.Path.join(tmpDir, 'test.rdf');
yield OS.File.copy(OS.Path.join(getTestDataDirectory().path, 'book_and_snapshot.rdf'), rdfFile);
yield OS.File.makeDir(OS.Path.join(tmpDir, 'files'));
yield OS.File.makeDir(OS.Path.join(tmpDir, 'files', 2));
yield OS.File.copy(
OS.Path.join(getTestDataDirectory().path, 'test.html'),
OS.Path.join(tmpDir, 'files', 2, 'test.html')
);
var promise = waitForItemEvent('add');
Zotero.debug(yield Zotero.File.getContentsAsync(rdfFile));
yield win.Zotero_File_Interface.importFile(Zotero.File.pathToFile(rdfFile))
var ids = yield promise;
assert.lengthOf(ids, 1);
// Check book
var item = Zotero.Items.get(ids[0]);
assert.equal(item.itemTypeID, Zotero.ItemTypes.getID('book'));
// Check attachment
var ids = item.getAttachments();
assert.lengthOf(ids, 1);
var attachment = Zotero.Items.get(ids[0]);
assert.equal(attachment.attachmentCharset, 'utf-8');
// Check indexing
var matches = yield Zotero.Fulltext.findTextInItems([attachment.id], 'test');
assert.lengthOf(matches, 1);
assert.propertyVal(matches[0], 'id', attachment.id);
});
}); });