From baa47e3bb96e1ca22f729c7e16f01c281a19a7fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adomas=20Ven=C4=8Dkauskas?= Date: Tue, 25 Aug 2026 15:54:37 +0300 Subject: [PATCH] Remove bibliography when last citation is deleted. Closes #3474 --- chrome/content/zotero/xpcom/integration.js | 22 ++++++++++++++-------- test/tests/integrationTest.js | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/chrome/content/zotero/xpcom/integration.js b/chrome/content/zotero/xpcom/integration.js index a474dde978..bc4d830f26 100644 --- a/chrome/content/zotero/xpcom/integration.js +++ b/chrome/content/zotero/xpcom/integration.js @@ -1333,13 +1333,19 @@ Zotero.Integration.Session.prototype._updateDocument = async function (forceCita var indicesToUpdate = Object.keys(this.processIndices); // Add bibliography indices to the above indices - if (this.bibliography // if bibliography exists - && Object.keys(this.citationsByIndex).length // and doc has citations - && (this.bibliographyHasChanged // and bibliography changed - || forceBibliography)) { // or if we should generate regardless of - // changes - for (let field of this._bibliographyFields) { - indicesToUpdate.push(field.index); + if (this.bibliography) { // if bibliography exists + if (!Object.keys(this.citationsByIndex).length) { // and there are no citations + for (let field of this._bibliographyFields) { + // Remove all bibliography fields + this._deleteFields[field.index] = true; + } + } + else if (this.bibliographyHasChanged // if bibliography changed + || forceBibliography) { // or force flag is active + for (let field of this._bibliographyFields) { + // Update all bibliography fields + indicesToUpdate.push(field.index); + } } } @@ -1496,7 +1502,7 @@ Zotero.Integration.Session.prototype._updateDocument = async function (forceCita var deleteFields = Object.keys(this._deleteFields).sort((a, b) => b - a); for (let fieldIndex of deleteFields) { - this._fields[fieldIndex].delete(); + await this._fields[fieldIndex].delete(); } this.processIndices = {} } diff --git a/test/tests/integrationTest.js b/test/tests/integrationTest.js index 3427d95ca2..d137045c40 100644 --- a/test/tests/integrationTest.js +++ b/test/tests/integrationTest.js @@ -1088,6 +1088,21 @@ describe("Zotero.Integration", function () { }); describe('#refresh', function () { + it('should delete the bibliography when the last citation is removed', async function () { + var docID = this.test.fullTitle(); + setAddEditItems(testItems[0]); + await initDoc(docID); + await execCommand('addEditCitation', docID); + await execCommand('addEditBibliography', docID); + var doc = applications[docID].doc; + assert.equal(doc.fields.length, 2); + + await doc.fields[0].delete(); + await execCommand('refresh', docID); + + assert.equal(doc.fields.length, 0); + }); + it ('should properly disambiguate author after editing in the database', async function () { var docID = this.test.fullTitle(); let testItem1 = await createDataObject('item', {libraryID: Zotero.Libraries.userLibraryID});