From 685be929237108bc4a1a125ca5c85e5c8ac758d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Wed, 1 Dec 2021 09:48:24 +0100 Subject: [PATCH] Support multiple spaces after msgid --- dist/pofile.js | 2 +- dist/pofile.min.js | 2 +- lib/po.js | 2 +- test/fixtures/no_header_extra_spaces.po | 8 ++++++++ test/headers.js | 20 ++++++++++++++++++++ 5 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/no_header_extra_spaces.po diff --git a/dist/pofile.js b/dist/pofile.js index 98f1838..a754945 100644 --- a/dist/pofile.js +++ b/dist/pofile.js @@ -83,7 +83,7 @@ PO.parse = function (data) { var headers = []; //everything until the first 'msgid ""' is considered header while (sections[0] && (headers.length === 0 || headers[headers.length - 1].indexOf('msgid ""') < 0)) { - if (sections[0].match(/msgid "[^"]/)) { + if (sections[0].match(/msgid\s+"[^"]/)) { //found first real string, adding a dummy header item headers.push('msgid ""'); } else { diff --git a/dist/pofile.min.js b/dist/pofile.min.js index b30d736..e4a88bc 100644 --- a/dist/pofile.min.js +++ b/dist/pofile.min.js @@ -1 +1 @@ -require=function(){function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c||a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g0&&(o>=p&&(l.obsolete=!0),o=0,p=0,f.items.push(l),l=new e.Item({nplurals:k}))}function c(a){return a=d(a),a=a.replace(/^[^"]*"|"$/g,""),a=a.replace(/\\([abtnvfr'"\\?]|([0-7]{3})|x([0-9a-fA-F]{2}))/g,function(a,b,c,d){if(c)return String.fromCharCode(parseInt(c,8));if(d)return String.fromCharCode(parseInt(d,16));switch(b){case"a":return"";case"b":return"\b";case"t":return"\t";case"n":return"\n";case"v":return"\v";case"f":return"\f";case"r":return"\r";default:return b}})}a=a.replace(/\r\n/g,"\n");for(var f=new e,g=a.split(/\n\n/),h=[];g[0]&&(0===h.length||h[h.length-1].indexOf('msgid ""')<0);)g[0].match(/msgid "[^"]/)?h.push('msgid ""'):h.push(g.shift());h=h.join("\n");var i=g.join("\n").split(/\n/);f.headers={"Project-Id-Version":"","Report-Msgid-Bugs-To":"","POT-Creation-Date":"","PO-Revision-Date":"","Last-Translator":"",Language:"","Language-Team":"","Content-Type":"","Content-Transfer-Encoding":"","Plural-Forms":""},f.headerOrder=[],h.split(/\n/).reduce(function(a,b){return a.merge&&(b=a.pop().slice(0,-1)+b.slice(1),delete a.merge),/^".*"$/.test(b)&&!/^".*\\n"$/.test(b)&&(a.merge=!0),a.push(b),a},[]).forEach(function(a){if(a.match(/^#\./))f.extractedComments.push(a.replace(/^#\.\s*/,""));else if(a.match(/^#/))f.comments.push(a.replace(/^#\s*/,""));else if(a.match(/^"/)){a=a.trim().replace(/^"/,"").replace(/\\n"$/,"");var b=a.split(/:/),c=b.shift().trim(),d=b.join(":").trim();f.headers[c]=d,f.headerOrder.push(c)}});for(var j=e.parsePluralForms(f.headers["Plural-Forms"]),k=j.nplurals,l=new e.Item({nplurals:k}),m=null,n=0,o=0,p=0;i.length>0;){var q=d(i.shift()),r=!1;if(q.match(/^#\~/)&&(q=d(q.substring(2)),r=!0),q.match(/^#:/))b(),l.references.push(d(q.replace(/^#:/,"")));else if(q.match(/^#,/)){b();for(var s=d(q.replace(/^#,/,"")).split(","),t=0;t0&&(p++,"msgstr"===m?l.msgstr[n]+=c(q):"msgid"===m?l.msgid+=c(q):"msgid_plural"===m?l.msgid_plural+=c(q):"msgctxt"===m&&(l.msgctxt+=c(q)));r&&o++}return b(),f},e.parsePluralForms=function(a){var b=(a||"").split(";").reduce(function(a,b){var c=b.trim(),d=c.indexOf("="),e=c.substring(0,d).trim(),f=c.substring(d+1).trim();return a[e]=f,a},{});return{nplurals:b.nplurals,plural:b.plural}},e.Item=function(a){var b=a&&a.nplurals;this.msgid="",this.msgctxt=null,this.references=[],this.msgid_plural=null,this.msgstr=[],this.comments=[],this.extractedComments=[],this.flags={},this.obsolete=!1;var c=Number(b);this.nplurals=isNaN(c)?2:c},e.Item.prototype.toString=function(){var a=[],b=this,c=function(a){return a=a.replace(/[\x07\b\t\v\f\r"\\]/g,function(a){switch(a){case"":return"\\a";case"\b":return"\\b";case"\t":return"\\t";case"\v":return"\\v";case"\f":return"\\f";case"\r":return"\\r";default:return"\\"+a}})},d=function(a,b,d){var e=[],f=b.split(/\n/),g="undefined"!=typeof d?"["+d+"]":"";return f.length>1?(e.push(a+g+' ""'),f.forEach(function(a){e.push('"'+c(a)+'"')})):e.push(a+g+' "'+c(b)+'"'),e},e=function(a,b,c){for(var e=d(a,b,c),f=1;f0&&a.push("#, "+f.join(","));var g=this.obsolete?"#~ ":"";return["msgctxt","msgid","msgid_plural","msgstr"].forEach(function(c){var f=b[c];if(null!=f){var h=!1;if(Array.isArray(f)&&(h=f.some(function(a){return a})),Array.isArray(f)&&f.length>1)f.forEach(function(b,d){var f=e(c,b,d);a=a.concat(g+f.join("\n"+g))});else if(b.msgid_plural&&"msgstr"===c&&!h)for(var i=0;i0&&(o>=p&&(l.obsolete=!0),o=0,p=0,f.items.push(l),l=new e.Item({nplurals:k}))}function c(a){return a=d(a),a=a.replace(/^[^"]*"|"$/g,""),a=a.replace(/\\([abtnvfr'"\\?]|([0-7]{3})|x([0-9a-fA-F]{2}))/g,function(a,b,c,d){if(c)return String.fromCharCode(parseInt(c,8));if(d)return String.fromCharCode(parseInt(d,16));switch(b){case"a":return"";case"b":return"\b";case"t":return"\t";case"n":return"\n";case"v":return"\v";case"f":return"\f";case"r":return"\r";default:return b}})}a=a.replace(/\r\n/g,"\n");for(var f=new e,g=a.split(/\n\n/),h=[];g[0]&&(0===h.length||h[h.length-1].indexOf('msgid ""')<0);)g[0].match(/msgid\s+"[^"]/)?h.push('msgid ""'):h.push(g.shift());h=h.join("\n");var i=g.join("\n").split(/\n/);f.headers={"Project-Id-Version":"","Report-Msgid-Bugs-To":"","POT-Creation-Date":"","PO-Revision-Date":"","Last-Translator":"",Language:"","Language-Team":"","Content-Type":"","Content-Transfer-Encoding":"","Plural-Forms":""},f.headerOrder=[],h.split(/\n/).reduce(function(a,b){return a.merge&&(b=a.pop().slice(0,-1)+b.slice(1),delete a.merge),/^".*"$/.test(b)&&!/^".*\\n"$/.test(b)&&(a.merge=!0),a.push(b),a},[]).forEach(function(a){if(a.match(/^#\./))f.extractedComments.push(a.replace(/^#\.\s*/,""));else if(a.match(/^#/))f.comments.push(a.replace(/^#\s*/,""));else if(a.match(/^"/)){a=a.trim().replace(/^"/,"").replace(/\\n"$/,"");var b=a.split(/:/),c=b.shift().trim(),d=b.join(":").trim();f.headers[c]=d,f.headerOrder.push(c)}});for(var j=e.parsePluralForms(f.headers["Plural-Forms"]),k=j.nplurals,l=new e.Item({nplurals:k}),m=null,n=0,o=0,p=0;i.length>0;){var q=d(i.shift()),r=!1;if(q.match(/^#\~/)&&(q=d(q.substring(2)),r=!0),q.match(/^#:/))b(),l.references.push(d(q.replace(/^#:/,"")));else if(q.match(/^#,/)){b();for(var s=d(q.replace(/^#,/,"")).split(","),t=0;t0&&(p++,"msgstr"===m?l.msgstr[n]+=c(q):"msgid"===m?l.msgid+=c(q):"msgid_plural"===m?l.msgid_plural+=c(q):"msgctxt"===m&&(l.msgctxt+=c(q)));r&&o++}return b(),f},e.parsePluralForms=function(a){var b=(a||"").split(";").reduce(function(a,b){var c=b.trim(),d=c.indexOf("="),e=c.substring(0,d).trim(),f=c.substring(d+1).trim();return a[e]=f,a},{});return{nplurals:b.nplurals,plural:b.plural}},e.Item=function(a){var b=a&&a.nplurals;this.msgid="",this.msgctxt=null,this.references=[],this.msgid_plural=null,this.msgstr=[],this.comments=[],this.extractedComments=[],this.flags={},this.obsolete=!1;var c=Number(b);this.nplurals=isNaN(c)?2:c},e.Item.prototype.toString=function(){var a=[],b=this,c=function(a){return a=a.replace(/[\x07\b\t\v\f\r"\\]/g,function(a){switch(a){case"":return"\\a";case"\b":return"\\b";case"\t":return"\\t";case"\v":return"\\v";case"\f":return"\\f";case"\r":return"\\r";default:return"\\"+a}})},d=function(a,b,d){var e=[],f=b.split(/\n/),g="undefined"!=typeof d?"["+d+"]":"";return f.length>1?(e.push(a+g+' ""'),f.forEach(function(a){e.push('"'+c(a)+'"')})):e.push(a+g+' "'+c(b)+'"'),e},e=function(a,b,c){for(var e=d(a,b,c),f=1;f0&&a.push("#, "+f.join(","));var g=this.obsolete?"#~ ":"";return["msgctxt","msgid","msgid_plural","msgstr"].forEach(function(c){var f=b[c];if(null!=f){var h=!1;if(Array.isArray(f)&&(h=f.some(function(a){return a})),Array.isArray(f)&&f.length>1)f.forEach(function(b,d){var f=e(c,b,d);a=a.concat(g+f.join("\n"+g))});else if(b.msgid_plural&&"msgstr"===c&&!h)for(var i=0;i