forked from sheetjs/sheetjs
SheetJS
ecee362e00
- utf8 translation pushes to array rather than string concat - large_strings test actually runs now (rather than blowing up)
83 lines
2.4 KiB
JavaScript
83 lines
2.4 KiB
JavaScript
var attregexg=/(\w+)=((?:")([^"]*)(?:")|(?:')([^']*)(?:'))/g;
|
|
var attregex=/(\w+)=((?:")(?:[^"]*)(?:")|(?:')(?:[^']*)(?:'))/;
|
|
function parsexmltag(tag) {
|
|
var words = tag.split(/\s+/);
|
|
var z = {'0': words[0]};
|
|
if(words.length === 1) return z;
|
|
(tag.match(attregexg) || []).map(
|
|
function(x){var y=x.match(attregex); z[y[1]] = y[2].substr(1,y[2].length-2); });
|
|
return z;
|
|
}
|
|
|
|
function evert(obj) {
|
|
var o = {};
|
|
Object.keys(obj).forEach(function(k) { if(obj.hasOwnProperty(k)) o[obj[k]] = k; });
|
|
return o;
|
|
}
|
|
|
|
var encodings = {
|
|
'"': '"',
|
|
''': "'",
|
|
'>': '>',
|
|
'<': '<',
|
|
'&': '&'
|
|
};
|
|
var rencoding = evert(encodings);
|
|
var rencstr = "&<>'\"".split("");
|
|
|
|
// TODO: CP remap (need to read file version to determine OS)
|
|
function unescapexml(text){
|
|
var s = text + '';
|
|
for(var y in encodings) s = s.replace(new RegExp(y,'g'), encodings[y]);
|
|
return s.replace(/_x([0-9a-fA-F]*)_/g,function(m,c) {return _chr(parseInt(c,16));});
|
|
}
|
|
function escapexml(text){
|
|
var s = text + '';
|
|
rencstr.forEach(function(y){s=s.replace(new RegExp(y,'g'), rencoding[y]);});
|
|
return s;
|
|
}
|
|
|
|
|
|
function parsexmlbool(value, tag) {
|
|
switch(value) {
|
|
case '0': case 0: case 'false': case 'FALSE': return false;
|
|
case '1': case 1: case 'true': case 'TRUE': return true;
|
|
default: throw "bad boolean value " + value + " in "+(tag||"?");
|
|
}
|
|
}
|
|
|
|
var utf8read = function(orig) {
|
|
var out = [], i = 0, c = 0, c1 = 0, c2 = 0, c3 = 0;
|
|
while (i < orig.length) {
|
|
c = orig.charCodeAt(i++);
|
|
if (c < 128) out.push(_chr(c));
|
|
else {
|
|
c2 = orig.charCodeAt(i++);
|
|
if (c>191 && c<224) out.push(_chr((c & 31) << 6 | c2 & 63));
|
|
else {
|
|
c3 = orig.charCodeAt(i++);
|
|
out.push(_chr((c & 15) << 12 | (c2 & 63) << 6 | c3 & 63));
|
|
}
|
|
}
|
|
}
|
|
return out.join("");
|
|
};
|
|
|
|
// matches <foo>...</foo> extracts content
|
|
function matchtag(f,g) {return new RegExp('<'+f+'(?: xml:space="preserve")?>([^\u2603]*)</'+f+'>',(g||"")+"m");}
|
|
|
|
function parseVector(data) {
|
|
var h = parsexmltag(data);
|
|
|
|
var matches = data.match(new RegExp("<vt:" + h.baseType + ">(.*?)</vt:" + h.baseType + ">", 'g'))||[];
|
|
if(matches.length != h.size) throw "unexpected vector length " + matches.length + " != " + h.size;
|
|
var res = [];
|
|
matches.forEach(function(x) {
|
|
var v = x.replace(/<[/]?vt:variant>/g,"").match(/<vt:([^>]*)>(.*)</);
|
|
res.push({v:v[2], t:v[1]});
|
|
});
|
|
return res;
|
|
}
|
|
|
|
function isval(x) { return typeof x !== "undefined" && x !== null; }
|