diff --git a/test/tests/utilitiesTest.js b/test/tests/utilitiesTest.js index bb949d9..17ba91f 100644 --- a/test/tests/utilitiesTest.js +++ b/test/tests/utilitiesTest.js @@ -291,7 +291,21 @@ describe("Zotero.Utilities", function() { }); }); }); - + + describe("#capitalizeTitle()", function () { + it("should capitalize within HTML tags", function () { + let input = 'Foo bar foo bar'; + let expected = 'Foo Bar Foo Bar'; + assert.equal(Zotero.Utilities.capitalizeTitle(input, true), expected); + }); + + it("shouldn't alter attributes within HTML tags", function () { + let input = 'Foo bar foo'; + let expected = 'Foo Bar Foo'; + assert.equal(Zotero.Utilities.capitalizeTitle(input, true), expected); + }); + }); + describe("walkNoteDOM()", function () { it("should iterate subtrees in depth-first order and allow modifications", function () { let html = loadTestData('note.html'); diff --git a/utilities.js b/utilities.js index b54aaf0..6808118 100644 --- a/utilities.js +++ b/utilities.js @@ -1042,9 +1042,16 @@ var Utilities = { if (Zotero.Prefs && !Zotero.Prefs.get('capitalizeTitles') && !force) return string; if (!string) return ""; + // Remove HTML tags but remember their positions + let htmlTags = []; + let cleanedString = string.replace(/<[^>]+>/g, (match, offset) => { + htmlTags.push({ match, offset }); + return ""; + }); + // split words - var words = string.split(delimiterRegexp); - var isUpperCase = string.toUpperCase() == string; + var words = cleanedString.split(delimiterRegexp); + var isUpperCase = cleanedString.toUpperCase() == cleanedString; var newString = ""; var delimiterOffset = words[0].length; @@ -1084,6 +1091,11 @@ var Utilities = { newString += words[i]; } + // Reinsert HTML tags into their original positions + htmlTags.forEach(tag => { + newString = newString.substring(0, tag.offset) + tag.match + newString.substring(tag.offset); + }); + return newString; },