Skip to content

Commit 6bea47a

Browse files
allow reading base64 files from a dataURI scheme (#2763)
1 parent aea2157 commit 6bea47a

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

bits/04_base64.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ function Base64_encode_pass(input) {
4848
function Base64_decode(input) {
4949
var o = "";
5050
var c1 = 0, c2 = 0, c3 = 0, e1 = 0, e2 = 0, e3 = 0, e4 = 0;
51-
input = input.replace(/[^\w\+\/\=]/g, "");
51+
input = input.replace(/^data:([^\/]+\/[^\/]+)?;base64,/, '')
52+
.replace(/[^\w\+\/\=]/g, "");
53+
5254
for (var i = 0; i < input.length; ) {
5355
e1 = Base64_map.indexOf(input.charAt(i++));
5456
e2 = Base64_map.indexOf(input.charAt(i++));

modules/04_base64.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ function Base64_encode_pass(input) {
4848
function Base64_decode(input) {
4949
var o = "";
5050
var c1 = 0, c2 = 0, c3 = 0, e1 = 0, e2 = 0, e3 = 0, e4 = 0;
51-
input = input.replace(/[^\w\+\/\=]/g, "");
51+
input = input.replace(/^data:.+\/.+;base64\,/,'')
52+
.replace(/[^\w\+\/\=]/g, "")
53+
5254
for (var i = 0; i < input.length; ) {
5355
e1 = Base64_map.indexOf(input.charAt(i++));
5456
e2 = Base64_map.indexOf(input.charAt(i++));

modules/04_base64.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ function Base64_encode_pass(input: string): string {
4040
function Base64_decode(input: string): string {
4141
var o = "";
4242
var c1=0, c2=0, c3=0, e1=0, e2=0, e3=0, e4=0;
43-
input = input.replace(/[^\w\+\/\=]/g, "");
43+
input = input.replace(/^data:.+\/.+;base64\,/,'')
44+
.replace(/[^\w\+\/\=]/g, "")
4445
for(var i = 0; i < input.length;) {
4546
e1 = Base64_map.indexOf(input.charAt(i++));
4647
e2 = Base64_map.indexOf(input.charAt(i++));

test.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,31 @@ describe('input formats', function() {
688688
it('should read base64 strings', function() { artifax.forEach(function(p) {
689689
X.read(fs.readFileSync(p, 'base64'), {type: 'base64'});
690690
}); });
691+
692+
it('handles base64 within data URI scheme (gh-2762)', function() {
693+
// Arrange
694+
var fileInBase64 = 'TmFtZXMNCkhhZmV6DQpTYW0NCg==';
695+
var fileInBase64WithDataURIScheme = 'data:text/csv;base64,TmFtZXMNCkhhZmV6DQpTYW0NCg==';
696+
697+
// Act
698+
var workBookFromRawBase64 = X.read(fileInBase64, { type: 'base64' });
699+
var workBookFromBase64WithinDataURI = X.read(fileInBase64WithDataURIScheme, { type: 'base64' });
700+
701+
// Assert
702+
assert.deepStrictEqual(workBookFromRawBase64, workBookFromBase64WithinDataURI);
703+
});
704+
it('handles base64 where data URI has no media type (gh-2762)', function() {
705+
// Arrange
706+
var fileInBase64 = 'TmFtZXMNCkhhZmV6DQpTYW0NCg==';
707+
var fileInBase64WithDataURIScheme = 'data:;base64,TmFtZXMNCkhhZmV6DQpTYW0NCg==';
708+
709+
// Act
710+
var workBookFromRawBase64 = X.read(fileInBase64, { type: 'base64' });
711+
var workBookFromBase64WithinDataURI = X.read(fileInBase64WithDataURIScheme, { type: 'base64' });
712+
713+
// Assert
714+
assert.deepStrictEqual(workBookFromRawBase64, workBookFromBase64WithinDataURI);
715+
});
691716
if(typeof Uint8Array !== 'undefined') it('should read array', function() { artifax.forEach(function(p) {
692717
X.read(fs.readFileSync(p, 'binary').split("").map(function(x) { return x.charCodeAt(0); }), {type:'array'});
693718
}); });
@@ -1393,7 +1418,7 @@ describe('parse features', function() {
13931418
});
13941419

13951420
describe('data types formats', function() {[
1396-
['xlsx', paths.dtfxlsx],
1421+
['xlsx', paths.dtfxlsx]
13971422
].forEach(function(m) { it(m[0], function() {
13981423
var wb = X.read(fs.readFileSync(m[1]), {type: TYPE, cellDates: true});
13991424
var ws = wb.Sheets[wb.SheetNames[0]];
@@ -2109,7 +2134,7 @@ function plaintext_test(wb, raw) {
21092134
var sheet = wb.Sheets[wb.SheetNames[0]];
21102135
plaintext_val.forEach(function(x) {
21112136
var cell = get_cell(sheet, x[0]);
2112-
var tcval = x[2+(!!raw ? 1 : 0)];
2137+
var tcval = x[2+(raw ? 1 : 0)];
21132138
var type = raw ? 's' : x[1];
21142139
if(x.length == 1) { if(cell) { assert.equal(cell.t, 'z'); assert.ok(!cell.v); } return; }
21152140
assert.equal(cell.v, tcval); assert.equal(cell.t, type);
@@ -2201,8 +2226,8 @@ describe('CSV', function() {
22012226
var aoa = [
22022227
["3a", "3 a", "3 a-1"],
22032228
["3b", "3 b", "3 b-1"],
2204-
["3p", "3 P", "3 p-1"],
2205-
]
2229+
["3p", "3 P", "3 p-1"]
2230+
];
22062231
var ws = X.read(aoa.map(function(row) { return row.join(","); }).join("\n"), {type: "string", cellDates: true}).Sheets.Sheet1;
22072232
for(var R = 0; R < 3; ++R) {
22082233
assert.equal(get_cell(ws, "A" + (R+1)).v, aoa[R][0]);

0 commit comments

Comments
 (0)