diff --git a/CHANGELOG.md b/CHANGELOG.md index 378d1f3f998..0d3501585a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# ## [Unreleased] ### Changed +- URL field formatting is updated. All whitespace chars, located at the beginning/ending of the url, are trimmed automatically - We changed the behavior of the field formatting dialog such that the `bibtexkey` is not changed when formatting all fields or all text fields. - We added a "Move file to file directory and rename file" option for simultaneously moving and renaming of document file. [#4166](https://github.com/JabRef/jabref/issues/4166) - Use integrated graphics card instead of discrete on macOS [#4070](https://github.com/JabRef/jabref/issues/4070) diff --git a/src/main/java/org/jabref/gui/fieldeditors/UrlEditor.java b/src/main/java/org/jabref/gui/fieldeditors/UrlEditor.java index f004099dd00..85f460c2e44 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/UrlEditor.java +++ b/src/main/java/org/jabref/gui/fieldeditors/UrlEditor.java @@ -13,6 +13,7 @@ import org.jabref.gui.autocompleter.AutoCompleteSuggestionProvider; import org.jabref.gui.fieldeditors.contextmenu.EditorMenus; import org.jabref.logic.formatter.bibtexfields.CleanupURLFormatter; +import org.jabref.logic.formatter.bibtexfields.TrimWhitespaceFormatter; import org.jabref.logic.integrity.FieldCheckers; import org.jabref.model.entry.BibEntry; import org.jabref.preferences.JabRefPreferences; @@ -37,7 +38,7 @@ public UrlEditor(String fieldName, DialogService dialogService, AutoCompleteSugg // init paste handler for URLEditor to format pasted url link in textArea textArea.setPasteActionHandler(()-> - textArea.setText(new CleanupURLFormatter().format(textArea.getText()))); + textArea.setText(new CleanupURLFormatter().format(new TrimWhitespaceFormatter().format(textArea.getText())))); new EditorValidator(preferences).configureValidation(viewModel.getFieldValidator().getValidationStatus(), textArea); diff --git a/src/main/java/org/jabref/gui/fieldeditors/contextmenu/EditorMenus.java b/src/main/java/org/jabref/gui/fieldeditors/contextmenu/EditorMenus.java index 8068b8a3f4a..575403025f0 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/contextmenu/EditorMenus.java +++ b/src/main/java/org/jabref/gui/fieldeditors/contextmenu/EditorMenus.java @@ -87,7 +87,7 @@ public static Supplier> getDOIMenu(TextArea textArea) { } /** - * The default context menu with a specific menu item to cleanup URL. + * The default context menu with a specific menu item to cleanup URL. * * @param textArea text-area that this menu will be connected to * @return menu containing items of the default menu and an item to cleanup a URL diff --git a/src/main/java/org/jabref/logic/formatter/bibtexfields/TrimWhitespaceFormatter.java b/src/main/java/org/jabref/logic/formatter/bibtexfields/TrimWhitespaceFormatter.java new file mode 100644 index 00000000000..6fefcb15b40 --- /dev/null +++ b/src/main/java/org/jabref/logic/formatter/bibtexfields/TrimWhitespaceFormatter.java @@ -0,0 +1,38 @@ +package org.jabref.logic.formatter.bibtexfields; + +import java.util.Objects; + +import org.jabref.logic.l10n.Localization; +import org.jabref.model.cleanup.Formatter; + +/** + * Trim all whitespace characters(defined in java) in the string. + */ +public class TrimWhitespaceFormatter extends Formatter { + + @Override + public String getName() { + return Localization.lang("Trim whitespace characters"); + } + + @Override + public String getKey() { + return "trim_whitespace"; + } + + @Override + public String format(String value) { + Objects.requireNonNull(value); + return value.trim(); + } + + @Override + public String getDescription() { + return Localization.lang("Trim all whitespace characters in the field content."); + } + + @Override + public String getExampleInput() { + return "\r\n InCDMA\n\r "; + } +} diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index bff30cd317d..f403c7c34ad 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -1067,6 +1067,11 @@ This\ operation\ requires\ one\ or\ more\ entries\ to\ be\ selected.=This operat Toggle\ entry\ preview=Toggle entry preview Toggle\ groups\ interface=Toggle groups interface + +Trim\ all\ whitespace\ characters\ in\ the\ field\ content.=Trim all whitespace characters in the field content. + +Trim\ whitespace\ characters=Trim whitespace characters + Try\ different\ encoding=Try different encoding Unabbreviate\ journal\ names\ of\ the\ selected\ entries=Unabbreviate journal names of the selected entries diff --git a/src/test/java/org/jabref/logic/formatter/bibtexfields/TrimWhitespaceFormatterTest.java b/src/test/java/org/jabref/logic/formatter/bibtexfields/TrimWhitespaceFormatterTest.java new file mode 100644 index 00000000000..d0554334a0e --- /dev/null +++ b/src/test/java/org/jabref/logic/formatter/bibtexfields/TrimWhitespaceFormatterTest.java @@ -0,0 +1,61 @@ + +package org.jabref.logic.formatter.bibtexfields; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TrimWhitespaceFormatterTest { + + private TrimWhitespaceFormatter formatter; + + @BeforeEach + public void setUp() { + formatter = new TrimWhitespaceFormatter(); + } + + @Test + public void removeHorizontalTabulations() { + assertEquals("whitespace", formatter.format("\twhitespace")); + assertEquals("whitespace", formatter.format("whitespace\t")); + assertEquals("whitespace", formatter.format("\twhitespace\t\t")); + } + + @Test + public void removeLineFeeds() { + assertEquals("whitespace", formatter.format("\nwhitespace")); + assertEquals("whitespace", formatter.format("whitespace\n")); + assertEquals("whitespace", formatter.format("\nwhitespace\n\n")); + } + + @Test + public void removeFormFeeds() { + assertEquals("whitespace", formatter.format("\fwhitespace")); + assertEquals("whitespace", formatter.format("whitespace\f")); + assertEquals("whitespace", formatter.format("\fwhitespace\f\f")); + } + + @Test + public void removeCarriageReturnFeeds() { + assertEquals("whitespace", formatter.format("\rwhitespace")); + assertEquals("whitespace", formatter.format("whitespace\r")); + assertEquals("whitespace", formatter.format("\rwhitespace\r\r")); + } + + @Test + public void removeSeparatorSpaces() { + assertEquals("whitespace", formatter.format(" whitespace")); + assertEquals("whitespace", formatter.format("whitespace ")); + assertEquals("whitespace", formatter.format(" whitespace ")); + } + + @Test + public void removeMixedWhitespaceChars() { + assertEquals("whitespace", formatter.format(" \r\t\fwhitespace")); + assertEquals("whitespace", formatter.format("whitespace \n \r")); + assertEquals("whitespace", formatter.format(" \f\t whitespace \r \n")); + } + + +}