From ba9a2db4537f8d26e45a57059f67ef42290bdf1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20B=C3=A4ume?= Date: Tue, 22 Sep 2015 10:38:43 +0200 Subject: [PATCH 1/2] add examples for po files without any headers those should at least be fixed --- test/fixtures/no_header.po | 8 +++++++ test/headers.js | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 test/fixtures/no_header.po diff --git a/test/fixtures/no_header.po b/test/fixtures/no_header.po new file mode 100644 index 0000000..c88eafb --- /dev/null +++ b/test/fixtures/no_header.po @@ -0,0 +1,8 @@ +# some comment + +msgid "First id, no header" +msgstr "" + +msgid "A second string" +msgstr "" + diff --git a/test/headers.js b/test/headers.js index d3605c4..2584e88 100644 --- a/test/headers.js +++ b/test/headers.js @@ -28,3 +28,46 @@ describe('Headers', function () { assert.equal(Object.keys(po.headers).length, 12); }); }); + +describe('PO files with no headers', function () { + + it('Parses an empty string', function () { + var po = PO.parse(''); + assert.notEqual(po, null); + // all headers should be empty + for (var key in po.headers) { + assert.equal(po.headers[key], ''); + } + assert.equal(po.items.length, 0); + }); + + it('Parses a minimal example', function () { + var po = PO.parse('msgid "minimal PO"\nmsgstr ""'); + assert.notEqual(po, null); + // all headers should be empty + for (var key in po.headers) { + assert.equal(po.headers[key], ''); + } + assert.equal(po.items.length, 1); + }); + + describe('advanced example', function () { + var po; + + before(function (done) { + PO.load(__dirname + '/fixtures/no_header.po', function (err, result) { + assert.equal(err, null); + po = result; + done(); + }); + }); + + it('Parses the po file', function () { + assert.notEqual(po, null); + }); + + it('Finds all items', function () { + assert.equal(po.items.length, 2); + }); + }); +}); From 71bb04f0465906a2aba1cd80dda8df00afa7c12c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20B=C3=A4ume?= Date: Tue, 22 Sep 2015 10:40:49 +0200 Subject: [PATCH 2/2] add support for po files without headers basically fix all the examples. --- lib/po.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/po.js b/lib/po.js index 6e1e2cd..6407079 100644 --- a/lib/po.js +++ b/lib/po.js @@ -64,10 +64,15 @@ PO.parse = function (data) { data = data.replace(/\r\n/g, '\n'); var po = new PO(); var sections = data.split(/\n\n/); - var headers = [sections.shift()]; + var headers = []; //everything until the first 'msgid ""' is considered header - while (headers[headers.length - 1].indexOf('msgid ""') < 0) { - headers.push(sections.shift()); + while (sections[0] && (headers.length === 0 || headers[headers.length - 1].indexOf('msgid ""') < 0)) { + if (sections[0].match(/msgid "[^"]/)) { + //found first real string, adding a dummy header item + headers.push('msgid ""'); + } else { + headers.push(sections.shift()); + } } headers = headers.join('\n'); var lines = sections.join('\n').split(/\n/);