Skip to content

Commit 0137014

Browse files
authored
fix: btoa utf8 encoding/decoding error (#114)
add implementation of Base64 utility
1 parent 3fed301 commit 0137014

File tree

3 files changed

+147
-3
lines changed

3 files changed

+147
-3
lines changed

src/Base64.ts

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/**
2+
*
3+
* Base64 encode / decode
4+
* http://www.webtoolkit.info/javascript_base64.html
5+
*
6+
**/
7+
export const Base64 = {
8+
// private property
9+
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
10+
11+
// public method for encoding
12+
encode: function (input: string) {
13+
let output = "";
14+
let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
15+
let i = 0;
16+
17+
input = Base64._utf8_encode(input);
18+
19+
while (i < input.length) {
20+
chr1 = input.charCodeAt(i++);
21+
chr2 = input.charCodeAt(i++);
22+
chr3 = input.charCodeAt(i++);
23+
24+
enc1 = chr1 >> 2;
25+
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
26+
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
27+
enc4 = chr3 & 63;
28+
29+
if (isNaN(chr2)) {
30+
enc3 = enc4 = 64;
31+
} else if (isNaN(chr3)) {
32+
enc4 = 64;
33+
}
34+
35+
output =
36+
output +
37+
this._keyStr.charAt(enc1) +
38+
this._keyStr.charAt(enc2) +
39+
this._keyStr.charAt(enc3) +
40+
this._keyStr.charAt(enc4);
41+
} // Whend
42+
43+
return output;
44+
}, // End Function encode
45+
46+
// public method for decoding
47+
decode: function (input: string) {
48+
let output = "";
49+
let chr1, chr2, chr3;
50+
let enc1, enc2, enc3, enc4;
51+
let i = 0;
52+
53+
input = input.replace(/[^A-Za-z0-9+/=]/g, "");
54+
while (i < input.length) {
55+
enc1 = this._keyStr.indexOf(input.charAt(i++));
56+
enc2 = this._keyStr.indexOf(input.charAt(i++));
57+
enc3 = this._keyStr.indexOf(input.charAt(i++));
58+
enc4 = this._keyStr.indexOf(input.charAt(i++));
59+
60+
chr1 = (enc1 << 2) | (enc2 >> 4);
61+
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
62+
chr3 = ((enc3 & 3) << 6) | enc4;
63+
64+
output = output + String.fromCharCode(chr1);
65+
66+
if (enc3 != 64) {
67+
output = output + String.fromCharCode(chr2);
68+
}
69+
70+
if (enc4 != 64) {
71+
output = output + String.fromCharCode(chr3);
72+
}
73+
} // Whend
74+
75+
output = Base64._utf8_decode(output);
76+
77+
return output;
78+
}, // End Function decode
79+
80+
// private method for UTF-8 encoding
81+
_utf8_encode: function (string: string) {
82+
let utftext = "";
83+
string = string.replace(/\r\n/g, "\n");
84+
85+
for (let n = 0; n < string.length; n++) {
86+
const c = string.charCodeAt(n);
87+
88+
if (c < 128) {
89+
utftext += String.fromCharCode(c);
90+
} else if (c > 127 && c < 2048) {
91+
utftext += String.fromCharCode((c >> 6) | 192);
92+
utftext += String.fromCharCode((c & 63) | 128);
93+
} else {
94+
utftext += String.fromCharCode((c >> 12) | 224);
95+
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
96+
utftext += String.fromCharCode((c & 63) | 128);
97+
}
98+
} // Next n
99+
100+
return utftext;
101+
}, // End Function _utf8_encode
102+
103+
// private method for UTF-8 decoding
104+
_utf8_decode: function (utftext: string) {
105+
let string = "";
106+
let i = 0;
107+
let c, c1, c2, c3;
108+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
109+
c = c1 = c2 = 0;
110+
111+
while (i < utftext.length) {
112+
c = utftext.charCodeAt(i);
113+
114+
if (c < 128) {
115+
string += String.fromCharCode(c);
116+
i++;
117+
} else if (c > 191 && c < 224) {
118+
c2 = utftext.charCodeAt(i + 1);
119+
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
120+
i += 2;
121+
} else {
122+
c2 = utftext.charCodeAt(i + 1);
123+
c3 = utftext.charCodeAt(i + 2);
124+
string += String.fromCharCode(
125+
((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)
126+
);
127+
i += 3;
128+
}
129+
} // Whend
130+
131+
return string;
132+
}, // End Function _utf8_decode
133+
};

src/commands.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { FILE_SUFFIX, LINK_PREFIX, TASK } from "./constants";
22
import type pixelmatch from "pixelmatch";
3+
import { Base64 } from "./Base64";
34

45
declare global {
56
// eslint-disable-next-line @typescript-eslint/no-namespace
@@ -139,8 +140,13 @@ Cypress.Commands.add(
139140
if (res.error) {
140141
log.set(
141142
"message",
142-
`${res.message}\n[See comparison](${LINK_PREFIX}${btoa(
143-
JSON.stringify({ title, imgPath })
143+
`${res.message}\n[See comparison](${LINK_PREFIX}${Base64.encode(
144+
encodeURIComponent(
145+
JSON.stringify({
146+
title,
147+
imgPath,
148+
})
149+
)
144150
)})`
145151
);
146152
log.set("consoleProps", () => res);

src/support.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Base64 } from "./Base64";
12
import "./commands";
23
import { FILE_SUFFIX, LINK_PREFIX, OVERLAY_CLASS, TASK } from "./constants";
34

@@ -91,7 +92,11 @@ after(() => {
9192
if (!top) return false;
9293

9394
const { title, imgPath } = JSON.parse(
94-
atob(e.currentTarget.getAttribute("href").substring(LINK_PREFIX.length))
95+
decodeURIComponent(
96+
Base64.decode(
97+
e.currentTarget.getAttribute("href").substring(LINK_PREFIX.length)
98+
)
99+
)
95100
);
96101
queueClear();
97102

0 commit comments

Comments
 (0)