From 20ca8edf87d61a6868e1b8d2c8a366fe1cb3d227 Mon Sep 17 00:00:00 2001
From: Dan Stillman <dstillman@zotero.org>
Date: Tue, 2 Jun 2015 20:32:31 -0400
Subject: [PATCH] Fix getContentsAsync() with invalid characters

---
 chrome/content/zotero/xpcom/file.js |  4 ++--
 test/tests/data/invalidChar.txt     |  1 +
 test/tests/data/utf8Char.txt        |  1 +
 test/tests/fileTest.js              | 16 ++++++++++++++++
 4 files changed, 20 insertions(+), 2 deletions(-)
 create mode 100644 test/tests/data/invalidChar.txt
 create mode 100644 test/tests/data/utf8Char.txt

diff --git a/chrome/content/zotero/xpcom/file.js b/chrome/content/zotero/xpcom/file.js
index 5f61efdbd..06e45b5eb 100644
--- a/chrome/content/zotero/xpcom/file.js
+++ b/chrome/content/zotero/xpcom/file.js
@@ -210,9 +210,9 @@ Zotero.File = new function(){
 		
 		var options = {
 			charset: charset ? charset : "UTF-8",
-			// This doesn't seem to work -- reading an image file still throws NS_ERROR_ILLEGAL_INPUT
-			replacement: "\uFFFD"
+			replacement: 65533
 		};
+		
 		var deferred = Zotero.Promise.defer();
 		NetUtil.asyncFetch(source, function(inputStream, status) {
 			if (!Components.isSuccessCode(status)) {
diff --git a/test/tests/data/invalidChar.txt b/test/tests/data/invalidChar.txt
new file mode 100644
index 000000000..77ce55973
--- /dev/null
+++ b/test/tests/data/invalidChar.txt
@@ -0,0 +1 @@
+A�B
\ No newline at end of file
diff --git a/test/tests/data/utf8Char.txt b/test/tests/data/utf8Char.txt
new file mode 100644
index 000000000..222eb3b17
--- /dev/null
+++ b/test/tests/data/utf8Char.txt
@@ -0,0 +1 @@
+A犬B
\ No newline at end of file
diff --git a/test/tests/fileTest.js b/test/tests/fileTest.js
index f30f869b1..4d2951cac 100644
--- a/test/tests/fileTest.js
+++ b/test/tests/fileTest.js
@@ -4,6 +4,22 @@ describe("Zotero.File", function () {
 			var path = OS.Path.join(getTestDataDirectory().path, "empty");
 			assert.equal((yield Zotero.File.getContentsAsync(path)), "");
 		})
+		
+		it("should handle an extended character", function* () {
+			var contents = yield Zotero.File.getContentsAsync(
+				OS.Path.join(getTestDataDirectory().path, "utf8Char.txt")
+			);
+			assert.lengthOf(contents, 3);
+			assert.equal(contents, "A\u72acB");
+		})
+		
+		it("should handle an invalid character", function* () {
+			var contents = yield Zotero.File.getContentsAsync(
+				OS.Path.join(getTestDataDirectory().path, "invalidChar.txt")
+			);
+			assert.lengthOf(contents, 3);
+			assert.equal(contents, "A\uFFFDB");
+		})
 	})
 	
 	describe("#copyDirectory()", function () {