From 20d0f103fd98b085e8c030ff634ef03e4872a300 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 10 Aug 2022 00:52:16 -0400 Subject: [PATCH] Automatically substitute `event-title` for `event` in styles Stopgap until styles are updated in citation-style-language/styles#6151 --- chrome/content/zotero/xpcom/style.js | 28 +++++++++++++++++++ test/tests/styleTest.js | 41 ++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/chrome/content/zotero/xpcom/style.js b/chrome/content/zotero/xpcom/style.js index 5cb6a6d236..5e24114e0a 100644 --- a/chrome/content/zotero/xpcom/style.js +++ b/chrome/content/zotero/xpcom/style.js @@ -762,6 +762,8 @@ Zotero.Style.prototype.getCiteProc = function(locale, format, automaticJournalAb var xml = this.getXML(); } + xml = this._eventToEventTitle(xml); + try { var citeproc; if (Zotero.Prefs.get('cite.useCiteprocRs')) { @@ -801,6 +803,32 @@ Zotero.Style.prototype.getCiteProc = function(locale, format, automaticJournalAb } }; +Zotero.Style.prototype._eventToEventTitle = function (xml) { + var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"] + .createInstance(Components.interfaces.nsIDOMParser); + var doc = parser.parseFromString(xml, "text/xml"); + if (doc.querySelector('[variable*="event-title"]')) { + return xml; + } + var elems = doc.querySelectorAll('[variable*="event"]'); + if (!elems.length) { + return xml; + } + var changed = false; + for (let elem of elems) { + let variable = elem.getAttribute('variable'); + if (!/event( |$)/.test(variable)) { + continue; + } + elem.setAttribute('variable', variable.replace(/event(?= |$)/, 'event-title')); + changed = true; + } + if (changed) { + xml = doc.documentElement.outerHTML; + } + return xml; +}; + Zotero.Style.prototype.__defineGetter__("class", /** * Retrieves the style class, either from the metadata that's already loaded or by loading the file diff --git a/test/tests/styleTest.js b/test/tests/styleTest.js index e324cd63c7..e493a3bd7d 100644 --- a/test/tests/styleTest.js +++ b/test/tests/styleTest.js @@ -83,4 +83,45 @@ describe("Zotero.Styles", function() { assert.equal(o.text, '1. Foo bar: baz qux. 2019; \n'); }); }); + + describe("event-title replacement", function () { + var item; + var eventStyleXML = ` + + `; + + before(async function () { + item = createUnsavedDataObject( + 'item', + { + itemType: 'conferencePaper', + title: 'Conference Paper' + } + ); + item.setField('conferenceName', 'Conference'); + await item.saveTx(); + }); + + it("should substitute `event-title` in style using `event`", function () { + var style = new Zotero.Style(eventStyleXML); + var cslEngine = style.getCiteProc('en-US', 'text'); + var text = Zotero.Cite.makeFormattedBibliographyOrCitationList(cslEngine, [item], "text"); + cslEngine.free(); + assert.equal(text, 'Conference - Conference\n'); + }); + }); });