Skip to content

Commit

Permalink
Disallow path characters as input as document title
Browse files Browse the repository at this point in the history
  • Loading branch information
gsantner committed Nov 18, 2017
1 parent cd88bb6 commit 89d7cb4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
### v0.2.3
- Select file/image from filesystem
- Fix relative web local file loading
- Added: manually save option
- Added: Launcher shortcuts
- Filesystem: Add refresh menu option

### v0.2.2
- Show document and file amount below folders
- Settings toolbar option
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
Expand Down Expand Up @@ -129,6 +130,8 @@ protected void onCreate(Bundle savedInstanceState) {
showEditor(null, file, fileIsFolder);
}
}

_toolbarTitleEdit.setFilters(new InputFilter[]{DocumentIO.INPUT_FILTER_FILESYSTEM_FILENAME});
}

@Override
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/net/gsantner/markor/util/DocumentIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;

import net.gsantner.markor.R;
import net.gsantner.markor.format.converter.MarkdownConverter;
Expand Down Expand Up @@ -149,4 +152,17 @@ public static String normalizeTitleForFilename(Document _document) {
}
return name;
}

public static final InputFilter INPUT_FILTER_FILESYSTEM_FILENAME = new InputFilter() {
private final String blockCharacterSet = "\\/:\"´`'*?<>\n\r@|";

public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int i = 0; !TextUtils.isEmpty(source) && i < source.length(); i++) {
if (blockCharacterSet.contains(("" + source.charAt(i)))) {
return "";
}
}
return null;
}
};
}
23 changes: 21 additions & 2 deletions app/src/test/java/net/gsantner/markor/model/DocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
*/
package net.gsantner.markor.model;

import net.gsantner.markor.model.Document;

import org.junit.Test;

import static net.gsantner.markor.util.DocumentIO.normalizeTitleForFilename;
import static org.assertj.core.api.Assertions.assertThat;

public class DocumentTest {
Expand Down Expand Up @@ -48,4 +47,24 @@ public void documentNewerVersion() {
assertThat(document.getTitle()).isEqualTo("Hello World Again");
assertThat(document.canGoToNewerVersion()).isEqualTo(false);
}

@Test
public void filenameNormalization(){
assertThat(normalizeTitleForFilename(nd(null, "HelloWorld"))).isEqualTo("HelloWorld");
assertThat(normalizeTitleForFilename(nd("HelloWorld", "text"))).isEqualTo("HelloWorld");
assertThat(normalizeTitleForFilename(nd(null, "text\nnewline"))).isEqualTo("text");
assertThat(normalizeTitleForFilename(nd(null, "sumtext/folder"))).isEqualTo("sumtextfolder");
assertThat(normalizeTitleForFilename(nd(null, "## hello world"))).isEqualTo("hello world");
}

private Document nd(String title, String content){
Document document = new Document();
if (title != null){
document.setTitle(title);
}
if (content != null){
document.setContent(content);
}
return document;
}
}

0 comments on commit 89d7cb4

Please sign in to comment.