Skip to content

Commit

Permalink
enforcing 31 character sheet name restriction
Browse files Browse the repository at this point in the history
  • Loading branch information
SheetJSDev committed May 31, 2014
1 parent f634261 commit d882757
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 49 deletions.
1 change: 0 additions & 1 deletion bits/36_xmlutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ var rencstr = "&<>'\"".split("");
function unescapexml(text){
var s = text + '';
s = s.replace(/&quot;/g, '"').replace(/&apos;/g, "'").replace(/&gt;/g, ">").replace(/&lt;/g, "<").replace(/&amp;/g, "&");
//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){
Expand Down
26 changes: 16 additions & 10 deletions bits/72_wsxml.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,37 @@ function parse_ws_xml(data, opts, rels) {
}

var refguess = {s: {r:1000000, c:1000000}, e: {r:0, c:0} };
var q = (opts.cellFormula ? ["v","f"] : ["v"]);
var sidx = 0;

var match_v = matchtag("v"), match_f = matchtag("f");
/* 18.3.1.80 sheetData CT_SheetData ? */
if((mtch=data.match(/<(?:\w+:)?sheetData>([^\u2603]*)<\/(?:\w+:)?sheetData>/m))) mtch[1].split(/<\/(?:\w+:)?row>/).forEach(function(x) {
if(x === "" || x.trim() === "") return;
mtch=data.match(/<(?:\w+:)?sheetData>([^\u2603]*)<\/(?:\w+:)?sheetData>/m);
if(mtch) for(var marr = mtch[1].split(/<\/(?:\w+:)?row>/), mt = 0; mt != marr.length; ++mt) {
x = marr[mt];
if(x === "" || x.trim() === "") continue;

/* 18.3.1.73 row CT_Row */
var row = parsexmltag(x.match(/<(?:\w+:)?row[^>]*>/)[0]);
if(opts.sheetRows && opts.sheetRows < +row.r) return;
if(opts.sheetRows && opts.sheetRows < +row.r) continue;
if(refguess.s.r > row.r - 1) refguess.s.r = row.r - 1;
if(refguess.e.r < row.r - 1) refguess.e.r = row.r - 1;
/* 18.3.1.4 c CT_Cell */
var cells = x.substr(x.indexOf('>')+1).split(/<(?:\w+:)?c /);
cells.forEach(function(c, idx) { if(c === "" || c.trim() === "") return;
var cref = c.match(/r=["']([^"']*)["']/);
for(var ix = 0, c=cells[0]; ix != cells.length; ++ix,c=cells[ix]) {
if(c === "" || c.trim() === "") continue;
var cref = c.match(/r=["']([^"']*)["']/), idx = ix;
c = "<c " + c;
if(cref && cref.length == 2) idx = decode_cell(cref[1]).c;
var cell = parsexmltag((c.match(/<c[^>]*>/)||[c])[0]); delete cell[0];
var d = c.substr(c.indexOf('>')+1);
var p = {};
q.forEach(function(f){var x=d.match(matchtag(f));if(x)p[f]=unescapexml(x[1]);});

var x=d.match(match_v);if(x)p.v=unescapexml(x[1]);
if(opts.cellFormula) {x=d.match(match_f);if(x)p.f=unescapexml(x[1]);}

/* SCHEMA IS ACTUALLY INCORRECT HERE. IF A CELL HAS NO T, EMIT "" */
if(cell.t === undefined && p.v === undefined) {
if(!opts.sheetStubs) return;
if(!opts.sheetStubs) continue;
p.t = "str"; p.v = undefined;
}
else p.t = (cell.t ? cell.t : "n"); // default is "n" in schema
Expand Down Expand Up @@ -93,8 +99,8 @@ function parse_ws_xml(data, opts, rels) {
}
} catch(e) { if(opts.WTF) throw e; }
s[cell.r] = p;
});
});
};
}

/* 18.3.1.48 hyperlinks CT_Hyperlinks */
if(data.match(/<\/hyperlinks>/)) data.match(/<hyperlink[^>]*\/>/g).forEach(function(h) {
Expand Down
2 changes: 1 addition & 1 deletion bits/77_wbxml.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ var write_wb_xml = function(wb, opts) {
o.push("<sheets>");
var i = 1;
wb.SheetNames.forEach(function(s) {
o.push(writextag('sheet',null,{name:s, sheetId:String(i), "r:id":"rId"+i}));
o.push(writextag('sheet',null,{name:s.substr(0,31), sheetId:String(i), "r:id":"rId"+i}));
++i;
});
o.push("</sheets>");
Expand Down
2 changes: 1 addition & 1 deletion bits/78_wbbin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var write_BrtBundleSh = function(data, o) {
o.write_shift(4, data.hsState);
o.write_shift(4, data.iTabID);
write_RelID(data.strRelID, o);
write_XLWideString(data.name, o);
write_XLWideString(data.name.substr(0,31), o);
return o;
};

Expand Down
4 changes: 2 additions & 2 deletions dist/xlsx.core.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/xlsx.core.min.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/xlsx.full.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/xlsx.full.min.map

Large diffs are not rendered by default.

31 changes: 18 additions & 13 deletions dist/xlsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,6 @@ var rencstr = "&<>'\"".split("");
function unescapexml(text){
var s = text + '';
s = s.replace(/&quot;/g, '"').replace(/&apos;/g, "'").replace(/&gt;/g, ">").replace(/&lt;/g, "<").replace(/&amp;/g, "&");
//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){
Expand Down Expand Up @@ -2506,31 +2505,37 @@ function parse_ws_xml(data, opts, rels) {
}

var refguess = {s: {r:1000000, c:1000000}, e: {r:0, c:0} };
var q = (opts.cellFormula ? ["v","f"] : ["v"]);
var sidx = 0;

var match_v = matchtag("v"), match_f = matchtag("f");
/* 18.3.1.80 sheetData CT_SheetData ? */
if((mtch=data.match(/<(?:\w+:)?sheetData>([^\u2603]*)<\/(?:\w+:)?sheetData>/m))) mtch[1].split(/<\/(?:\w+:)?row>/).forEach(function(x) {
if(x === "" || x.trim() === "") return;
mtch=data.match(/<(?:\w+:)?sheetData>([^\u2603]*)<\/(?:\w+:)?sheetData>/m);
if(mtch) for(var marr = mtch[1].split(/<\/(?:\w+:)?row>/), mt = 0; mt != marr.length; ++mt) {
x = marr[mt];
if(x === "" || x.trim() === "") continue;

/* 18.3.1.73 row CT_Row */
var row = parsexmltag(x.match(/<(?:\w+:)?row[^>]*>/)[0]);
if(opts.sheetRows && opts.sheetRows < +row.r) return;
if(opts.sheetRows && opts.sheetRows < +row.r) continue;
if(refguess.s.r > row.r - 1) refguess.s.r = row.r - 1;
if(refguess.e.r < row.r - 1) refguess.e.r = row.r - 1;
/* 18.3.1.4 c CT_Cell */
var cells = x.substr(x.indexOf('>')+1).split(/<(?:\w+:)?c /);
cells.forEach(function(c, idx) { if(c === "" || c.trim() === "") return;
var cref = c.match(/r=["']([^"']*)["']/);
for(var ix = 0, c=cells[0]; ix != cells.length; ++ix,c=cells[ix]) {
if(c === "" || c.trim() === "") continue;
var cref = c.match(/r=["']([^"']*)["']/), idx = ix;
c = "<c " + c;
if(cref && cref.length == 2) idx = decode_cell(cref[1]).c;
var cell = parsexmltag((c.match(/<c[^>]*>/)||[c])[0]); delete cell[0];
var d = c.substr(c.indexOf('>')+1);
var p = {};
q.forEach(function(f){var x=d.match(matchtag(f));if(x)p[f]=unescapexml(x[1]);});

var x=d.match(match_v);if(x)p.v=unescapexml(x[1]);
if(opts.cellFormula) {x=d.match(match_f);if(x)p.f=unescapexml(x[1]);}

/* SCHEMA IS ACTUALLY INCORRECT HERE. IF A CELL HAS NO T, EMIT "" */
if(cell.t === undefined && p.v === undefined) {
if(!opts.sheetStubs) return;
if(!opts.sheetStubs) continue;
p.t = "str"; p.v = undefined;
}
else p.t = (cell.t ? cell.t : "n"); // default is "n" in schema
Expand Down Expand Up @@ -2581,8 +2586,8 @@ function parse_ws_xml(data, opts, rels) {
}
} catch(e) { if(opts.WTF) throw e; }
s[cell.r] = p;
});
});
};
}

/* 18.3.1.48 hyperlinks CT_Hyperlinks */
if(data.match(/<\/hyperlinks>/)) data.match(/<hyperlink[^>]*\/>/g).forEach(function(h) {
Expand Down Expand Up @@ -3283,7 +3288,7 @@ var write_wb_xml = function(wb, opts) {
o.push("<sheets>");
var i = 1;
wb.SheetNames.forEach(function(s) {
o.push(writextag('sheet',null,{name:s, sheetId:String(i), "r:id":"rId"+i}));
o.push(writextag('sheet',null,{name:s.substr(0,31), sheetId:String(i), "r:id":"rId"+i}));
++i;
});
o.push("</sheets>");
Expand All @@ -3304,7 +3309,7 @@ var write_BrtBundleSh = function(data, o) {
o.write_shift(4, data.hsState);
o.write_shift(4, data.iTabID);
write_RelID(data.strRelID, o);
write_XLWideString(data.name, o);
write_XLWideString(data.name.substr(0,31), o);
return o;
};

Expand Down
2 changes: 1 addition & 1 deletion dist/xlsx.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/xlsx.min.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ describe('invalid files', function() {
].forEach(function(w) { it('should fail on ' + w[0], function() {
assert.throws(function() { X.readFile(dir + w[1]); });
assert.throws(function() { X.read(fs.readFileSync(dir+w[1], 'base64'), {type:'base64'}); });
}); });
}); });
});
describe('write', function() {
it('should pass', function() { X.write(X.readFile(paths.fst1), {type:'binary'}); });
Expand Down Expand Up @@ -680,7 +680,7 @@ function sheet_from_array_of_arrays(data, opts) {
for(var C = 0; C != data[R].length; ++C) {
if(range.s.r > R) range.s.r = R;
if(range.s.c > C) range.s.c = C;
if(range.e.r < R) range.e.r = R;
if(range.e.r < R) range.e.r = R;
if(range.e.c < C) range.e.c = C;
var cell = {v: data[R][C] };
if(cell.v == null) continue;
Expand Down
31 changes: 18 additions & 13 deletions xlsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,6 @@ var rencstr = "&<>'\"".split("");
function unescapexml(text){
var s = text + '';
s = s.replace(/&quot;/g, '"').replace(/&apos;/g, "'").replace(/&gt;/g, ">").replace(/&lt;/g, "<").replace(/&amp;/g, "&");
//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){
Expand Down Expand Up @@ -2506,31 +2505,37 @@ function parse_ws_xml(data, opts, rels) {
}

var refguess = {s: {r:1000000, c:1000000}, e: {r:0, c:0} };
var q = (opts.cellFormula ? ["v","f"] : ["v"]);
var sidx = 0;

var match_v = matchtag("v"), match_f = matchtag("f");
/* 18.3.1.80 sheetData CT_SheetData ? */
if((mtch=data.match(/<(?:\w+:)?sheetData>([^\u2603]*)<\/(?:\w+:)?sheetData>/m))) mtch[1].split(/<\/(?:\w+:)?row>/).forEach(function(x) {
if(x === "" || x.trim() === "") return;
mtch=data.match(/<(?:\w+:)?sheetData>([^\u2603]*)<\/(?:\w+:)?sheetData>/m);
if(mtch) for(var marr = mtch[1].split(/<\/(?:\w+:)?row>/), mt = 0; mt != marr.length; ++mt) {
x = marr[mt];
if(x === "" || x.trim() === "") continue;

/* 18.3.1.73 row CT_Row */
var row = parsexmltag(x.match(/<(?:\w+:)?row[^>]*>/)[0]);
if(opts.sheetRows && opts.sheetRows < +row.r) return;
if(opts.sheetRows && opts.sheetRows < +row.r) continue;
if(refguess.s.r > row.r - 1) refguess.s.r = row.r - 1;
if(refguess.e.r < row.r - 1) refguess.e.r = row.r - 1;
/* 18.3.1.4 c CT_Cell */
var cells = x.substr(x.indexOf('>')+1).split(/<(?:\w+:)?c /);
cells.forEach(function(c, idx) { if(c === "" || c.trim() === "") return;
var cref = c.match(/r=["']([^"']*)["']/);
for(var ix = 0, c=cells[0]; ix != cells.length; ++ix,c=cells[ix]) {
if(c === "" || c.trim() === "") continue;
var cref = c.match(/r=["']([^"']*)["']/), idx = ix;
c = "<c " + c;
if(cref && cref.length == 2) idx = decode_cell(cref[1]).c;
var cell = parsexmltag((c.match(/<c[^>]*>/)||[c])[0]); delete cell[0];
var d = c.substr(c.indexOf('>')+1);
var p = {};
q.forEach(function(f){var x=d.match(matchtag(f));if(x)p[f]=unescapexml(x[1]);});

var x=d.match(match_v);if(x)p.v=unescapexml(x[1]);
if(opts.cellFormula) {x=d.match(match_f);if(x)p.f=unescapexml(x[1]);}

/* SCHEMA IS ACTUALLY INCORRECT HERE. IF A CELL HAS NO T, EMIT "" */
if(cell.t === undefined && p.v === undefined) {
if(!opts.sheetStubs) return;
if(!opts.sheetStubs) continue;
p.t = "str"; p.v = undefined;
}
else p.t = (cell.t ? cell.t : "n"); // default is "n" in schema
Expand Down Expand Up @@ -2581,8 +2586,8 @@ function parse_ws_xml(data, opts, rels) {
}
} catch(e) { if(opts.WTF) throw e; }
s[cell.r] = p;
});
});
};
}

/* 18.3.1.48 hyperlinks CT_Hyperlinks */
if(data.match(/<\/hyperlinks>/)) data.match(/<hyperlink[^>]*\/>/g).forEach(function(h) {
Expand Down Expand Up @@ -3283,7 +3288,7 @@ var write_wb_xml = function(wb, opts) {
o.push("<sheets>");
var i = 1;
wb.SheetNames.forEach(function(s) {
o.push(writextag('sheet',null,{name:s, sheetId:String(i), "r:id":"rId"+i}));
o.push(writextag('sheet',null,{name:s.substr(0,31), sheetId:String(i), "r:id":"rId"+i}));
++i;
});
o.push("</sheets>");
Expand All @@ -3304,7 +3309,7 @@ var write_BrtBundleSh = function(data, o) {
o.write_shift(4, data.hsState);
o.write_shift(4, data.iTabID);
write_RelID(data.strRelID, o);
write_XLWideString(data.name, o);
write_XLWideString(data.name.substr(0,31), o);
return o;
};

Expand Down

0 comments on commit d882757

Please sign in to comment.