diff --git a/chrome/content/zotero/xpcom/data/search.js b/chrome/content/zotero/xpcom/data/search.js index 6be07e5686..84ab907226 100644 --- a/chrome/content/zotero/xpcom/data/search.js +++ b/chrome/content/zotero/xpcom/data/search.js @@ -1101,16 +1101,24 @@ Zotero.Search.prototype._buildQuery = Zotero.Promise.coroutine(function* () { // Exclude deleted items (and their child items) by default let not = deleted ? "" : "NOT "; - let op = deleted ? "OR" : "AND"; - sql += " WHERE (" - + `itemID ${not} IN (SELECT itemID FROM deletedItems) ` - + `${op} itemID ${not}IN (SELECT itemID FROM itemNotes ` + sql += ` WHERE (itemID ${not} IN (` + // Deleted items + + "SELECT itemID FROM deletedItems " + // Child notes of deleted items + + "UNION SELECT itemID FROM itemNotes " + "WHERE parentItemID IS NOT NULL AND " - + "parentItemID IN (SELECT itemID FROM deletedItems)) " - + `${op} itemID ${not}IN (SELECT itemID FROM itemAttachments ` + + "parentItemID IN (SELECT itemID FROM deletedItems) " + // Child attachments of deleted items + + "UNION SELECT itemID FROM itemAttachments " + "WHERE parentItemID IS NOT NULL AND " - + "parentItemID IN (SELECT itemID FROM deletedItems))" - + ")"; + + "parentItemID IN (SELECT itemID FROM deletedItems)" + // Annotations of deleted attachments + + "UNION SELECT itemID FROM itemAnnotations " + + "WHERE parentItemID IN (SELECT itemID FROM deletedItems)" + // Annotations of attachments of deleted items + + "UNION SELECT itemID FROM itemAnnotations " + + "WHERE parentItemID IN (SELECT itemID FROM itemAttachments WHERE parentItemID IN (SELECT itemID FROM deletedItems))" + + "))"; if (noChildren){ sql += " AND (itemID NOT IN (SELECT itemID FROM itemNotes " diff --git a/test/tests/searchTest.js b/test/tests/searchTest.js index 74c37b5f13..da791f1e2b 100644 --- a/test/tests/searchTest.js +++ b/test/tests/searchTest.js @@ -610,6 +610,36 @@ describe("Zotero.Search", function() { }); }); }); + + describe("deleted", function () { + describe("if not present", function () { + it("should not match regular items in trash with annotated child attachments", async function () { + var item = await createDataObject('item'); + item.deleted = true; + await item.saveTx(); + var attachment = await importPDFAttachment(item); + await createAnnotation('highlight', attachment); + + var s = new Zotero.Search(); + s.libraryID = userLibraryID; + var matches = await s.search(); + assert.notInclude(matches, attachment.id); + }); + + it("should not match regular items with annotated child attachments in trash", async function () { + var item = await createDataObject('item'); + var attachment = await importPDFAttachment(item); + attachment.deleted = true; + await attachment.saveTx(); + await createAnnotation('highlight', attachment); + + var s = new Zotero.Search(); + s.libraryID = userLibraryID; + var matches = await s.search(); + assert.notInclude(matches, attachment.id); + }); + }); + }); }); });