Skip to content

Commit d8f6c5c

Browse files
Songyu-WangSiedlerchr
authored andcommitted
Auto trim url field (#4355)
* add url field auto trim whitespace feature * add test, edit JabRef_en file and update change log * update tests name * fix ci error
1 parent cad9254 commit d8f6c5c

File tree

6 files changed

+108
-2
lines changed

6 files changed

+108
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
1111
## [Unreleased]
1212

1313
### Changed
14+
- URL field formatting is updated. All whitespace chars, located at the beginning/ending of the url, are trimmed automatically
1415
- We changed the behavior of the field formatting dialog such that the `bibtexkey` is not changed when formatting all fields or all text fields.
1516
- 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)
1617
- Use integrated graphics card instead of discrete on macOS [#4070](https://github.com/JabRef/jabref/issues/4070)

src/main/java/org/jabref/gui/fieldeditors/UrlEditor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.jabref.gui.autocompleter.AutoCompleteSuggestionProvider;
1414
import org.jabref.gui.fieldeditors.contextmenu.EditorMenus;
1515
import org.jabref.logic.formatter.bibtexfields.CleanupURLFormatter;
16+
import org.jabref.logic.formatter.bibtexfields.TrimWhitespaceFormatter;
1617
import org.jabref.logic.integrity.FieldCheckers;
1718
import org.jabref.model.entry.BibEntry;
1819
import org.jabref.preferences.JabRefPreferences;
@@ -37,7 +38,7 @@ public UrlEditor(String fieldName, DialogService dialogService, AutoCompleteSugg
3738

3839
// init paste handler for URLEditor to format pasted url link in textArea
3940
textArea.setPasteActionHandler(()->
40-
textArea.setText(new CleanupURLFormatter().format(textArea.getText())));
41+
textArea.setText(new CleanupURLFormatter().format(new TrimWhitespaceFormatter().format(textArea.getText()))));
4142

4243

4344
new EditorValidator(preferences).configureValidation(viewModel.getFieldValidator().getValidationStatus(), textArea);

src/main/java/org/jabref/gui/fieldeditors/contextmenu/EditorMenus.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static Supplier<List<MenuItem>> getDOIMenu(TextArea textArea) {
8787
}
8888

8989
/**
90-
* The default context menu with a specific menu item to cleanup URL.
90+
* The default context menu with a specific menu item to cleanup URL.
9191
*
9292
* @param textArea text-area that this menu will be connected to
9393
* @return menu containing items of the default menu and an item to cleanup a URL
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.jabref.logic.formatter.bibtexfields;
2+
3+
import java.util.Objects;
4+
5+
import org.jabref.logic.l10n.Localization;
6+
import org.jabref.model.cleanup.Formatter;
7+
8+
/**
9+
* Trim all whitespace characters(defined in java) in the string.
10+
*/
11+
public class TrimWhitespaceFormatter extends Formatter {
12+
13+
@Override
14+
public String getName() {
15+
return Localization.lang("Trim whitespace characters");
16+
}
17+
18+
@Override
19+
public String getKey() {
20+
return "trim_whitespace";
21+
}
22+
23+
@Override
24+
public String format(String value) {
25+
Objects.requireNonNull(value);
26+
return value.trim();
27+
}
28+
29+
@Override
30+
public String getDescription() {
31+
return Localization.lang("Trim all whitespace characters in the field content.");
32+
}
33+
34+
@Override
35+
public String getExampleInput() {
36+
return "\r\n InCDMA\n\r ";
37+
}
38+
}

src/main/resources/l10n/JabRef_en.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,11 @@ This\ operation\ requires\ one\ or\ more\ entries\ to\ be\ selected.=This operat
10671067

10681068
Toggle\ entry\ preview=Toggle entry preview
10691069
Toggle\ groups\ interface=Toggle groups interface
1070+
1071+
Trim\ all\ whitespace\ characters\ in\ the\ field\ content.=Trim all whitespace characters in the field content.
1072+
1073+
Trim\ whitespace\ characters=Trim whitespace characters
1074+
10701075
Try\ different\ encoding=Try different encoding
10711076

10721077
Unabbreviate\ journal\ names\ of\ the\ selected\ entries=Unabbreviate journal names of the selected entries
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
package org.jabref.logic.formatter.bibtexfields;
3+
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class TrimWhitespaceFormatterTest {
10+
11+
private TrimWhitespaceFormatter formatter;
12+
13+
@BeforeEach
14+
public void setUp() {
15+
formatter = new TrimWhitespaceFormatter();
16+
}
17+
18+
@Test
19+
public void removeHorizontalTabulations() {
20+
assertEquals("whitespace", formatter.format("\twhitespace"));
21+
assertEquals("whitespace", formatter.format("whitespace\t"));
22+
assertEquals("whitespace", formatter.format("\twhitespace\t\t"));
23+
}
24+
25+
@Test
26+
public void removeLineFeeds() {
27+
assertEquals("whitespace", formatter.format("\nwhitespace"));
28+
assertEquals("whitespace", formatter.format("whitespace\n"));
29+
assertEquals("whitespace", formatter.format("\nwhitespace\n\n"));
30+
}
31+
32+
@Test
33+
public void removeFormFeeds() {
34+
assertEquals("whitespace", formatter.format("\fwhitespace"));
35+
assertEquals("whitespace", formatter.format("whitespace\f"));
36+
assertEquals("whitespace", formatter.format("\fwhitespace\f\f"));
37+
}
38+
39+
@Test
40+
public void removeCarriageReturnFeeds() {
41+
assertEquals("whitespace", formatter.format("\rwhitespace"));
42+
assertEquals("whitespace", formatter.format("whitespace\r"));
43+
assertEquals("whitespace", formatter.format("\rwhitespace\r\r"));
44+
}
45+
46+
@Test
47+
public void removeSeparatorSpaces() {
48+
assertEquals("whitespace", formatter.format(" whitespace"));
49+
assertEquals("whitespace", formatter.format("whitespace "));
50+
assertEquals("whitespace", formatter.format(" whitespace "));
51+
}
52+
53+
@Test
54+
public void removeMixedWhitespaceChars() {
55+
assertEquals("whitespace", formatter.format(" \r\t\fwhitespace"));
56+
assertEquals("whitespace", formatter.format("whitespace \n \r"));
57+
assertEquals("whitespace", formatter.format(" \f\t whitespace \r \n"));
58+
}
59+
60+
61+
}

0 commit comments

Comments
 (0)