From 76adc351b75b2489f3ef009a7e39df3dabadb50d Mon Sep 17 00:00:00 2001 From: Chris Price Date: Mon, 18 Aug 2014 22:26:49 -0400 Subject: Remove potential Unicode BOM after reading XML file --- lib/docx/office-xml-reader.js | 3 +++ tests/mammoth.tests.js | 8 ++++++++ tests/test-data/bom.docx | Bin 0 -> 2836 bytes 3 files changed, 11 insertions(+) create mode 100644 tests/test-data/bom.docx diff --git a/lib/docx/office-xml-reader.js b/lib/docx/office-xml-reader.js index 39b700e..861b11e 100644 --- a/lib/docx/office-xml-reader.js +++ b/lib/docx/office-xml-reader.js @@ -22,6 +22,9 @@ function read(xmlString) { function readXmlFromZipFile(docxFile, path) { if (docxFile.exists(path)) { return docxFile.read(path, "utf-8") + .then(function (xmlString) { + return xmlString.replace(/^\uFEFF/g, ''); + }) .then(read); } else { return promises.resolve(null); diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js index e0c1f6c..9be7006 100644 --- a/tests/mammoth.tests.js +++ b/tests/mammoth.tests.js @@ -32,6 +32,14 @@ describe('mammoth', function() { }); }); + test('should read docx xml files with unicode byte order mark', function() { + var docxPath = path.join(__dirname, "test-data/bom.docx"); + return mammoth.convertToHtml({path: docxPath}).then(function (result) { + assert.equal(result.value, "

This XML has a byte order mark.

"); + assert.deepEqual(result.messages, []); + }); + }); + test('style map can be expressed as string', function() { var docxFile = createFakeDocxFile({ "word/document.xml": testData("simple/word/document.xml") diff --git a/tests/test-data/bom.docx b/tests/test-data/bom.docx new file mode 100644 index 0000000..3881238 Binary files /dev/null and b/tests/test-data/bom.docx differ -- cgit v1.2.3