From a2c97552a8925868749944935b7a050409b5cb58 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Tue, 28 Feb 2017 17:39:20 +0100 Subject: [PATCH 1/2] Do not count escaped braces for brace calculation --- .../logic/bibtex/LatexFieldFormatter.java | 18 +++++++++++++----- .../logic/bibtex/LatexFieldFormatterTests.java | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/jabref/logic/bibtex/LatexFieldFormatter.java b/src/main/java/org/jabref/logic/bibtex/LatexFieldFormatter.java index 74c896d0ebd..74ca79d9014 100644 --- a/src/main/java/org/jabref/logic/bibtex/LatexFieldFormatter.java +++ b/src/main/java/org/jabref/logic/bibtex/LatexFieldFormatter.java @@ -259,11 +259,19 @@ private static void checkBraces(String text) throws IllegalArgumentException { int current = -1; // First we collect all occurrences: - while ((current = text.indexOf('{', current + 1)) != -1) { - left.add(current); - } - while ((current = text.indexOf('}', current + 1)) != -1) { - right.add(current); + for (int i = 0; i < text.length(); i++) { + char item = text.charAt(i); + + boolean charBeforeIsEscape = false; + if(i > 0 && text.charAt(i - 1) == '\\') { + charBeforeIsEscape = true; + } + + if(!charBeforeIsEscape && item == '{') { + left.add(current); + } else if (!charBeforeIsEscape && item == '{') { + right.add(current); + } } // Then we throw an exception if the error criteria are met. diff --git a/src/test/java/org/jabref/logic/bibtex/LatexFieldFormatterTests.java b/src/test/java/org/jabref/logic/bibtex/LatexFieldFormatterTests.java index a8422abfd34..541604a68ca 100644 --- a/src/test/java/org/jabref/logic/bibtex/LatexFieldFormatterTests.java +++ b/src/test/java/org/jabref/logic/bibtex/LatexFieldFormatterTests.java @@ -81,4 +81,18 @@ public void removeWhitespaceFromNonMultiLineFields() throws Exception { assertEquals(expected, title); assertEquals(expected, any); } + + @Test(expected = IllegalArgumentException.class) + public void reportUnbalancedBracing() { + String unbalanced = "{"; + + formatter.format(unbalanced, "anyfield"); + } + + @Test(expected = IllegalArgumentException.class) + public void reportUnbalancedBracingWithEscapedBraces() { + String unbalanced = "{\\}"; + + formatter.format(unbalanced, "anyfield"); + } } From 47524f3af04403765c8b616f304c42c78a05f6dc Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Tue, 28 Feb 2017 17:41:23 +0100 Subject: [PATCH 2/2] Add changelog entry --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d073a77117..ba31c920e6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,7 +47,8 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - Sciencedirect/Elsevier fetcher is now able to scrape new HTML structure [#2576](https://github.com/JabRef/jabref/issues/2576) - Fixed the synchronization logic of keywords and special fields and vice versa [#2580](https://github.com/JabRef/jabref/issues/2580) - We fixed an issue where the "find unlinked files" functionality threw an error when only one PDF was imported but not assigned to an entry [#2577](https://github.com/JabRef/jabref/issues/2577) - + - We fixed issue where escaped braces were incorrectly counted when calculating brace balance in a field [#2561](https://github.com/JabRef/jabref/issues/2561) + ### Removed