This commit is contained in:
TheSavageTeddy 2025-04-14 18:46:44 +01:00 committed by GitHub
commit 62d2433e15
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -28,7 +28,12 @@ class URLEncode extends Operation {
"name": "Encode all special chars", "name": "Encode all special chars",
"type": "boolean", "type": "boolean",
"value": false "value": false
} },
{
"name": "Encode all chars",
"type": "boolean",
"value": false
},
]; ];
} }
@ -38,8 +43,19 @@ class URLEncode extends Operation {
* @returns {string} * @returns {string}
*/ */
run(input, args) { run(input, args) {
const encodeAll = args[0]; const encodeSpecial = args[0];
return encodeAll ? this.encodeAllChars(input) : encodeURI(input); const encodeAll = args[1];
return encodeAll ? this.encodeAllChars(input) : encodeSpecial ? this.encodeAllSpecialChars(input) : encodeURI(input);
}
/**
* Pads a string from the front to a given length with a given char
*
* @param {string} str
* @returns {string}
*/
frontPad (str, length, char) {
return str.length >= length ? str : (char * (length - str.length)) + str;
} }
/** /**
@ -48,19 +64,31 @@ class URLEncode extends Operation {
* @param {string} str * @param {string} str
* @returns {string} * @returns {string}
*/ */
encodeAllSpecialChars (str) {
const SPECIAL_CHARS = "!#'()*-._~";
let encoded = "";
for (const char of str) {
if (encodeURIComponent(char) === char && SPECIAL_CHARS.includes(char)) {
encoded += "%" + this.frontPad(char.charCodeAt(0).toString(16).toUpperCase(), 2, "0");
} else {
encoded += encodeURIComponent(char);
}
}
return encoded;
}
/**
* Encode ALL characters in URL including alphanumeric based on char codes
*
* @param {string} str
* @returns {string}
*/
encodeAllChars (str) { encodeAllChars (str) {
// TODO Do this programmatically let encoded = "";
return encodeURIComponent(str) for (const char of str) {
.replace(/!/g, "%21") encoded += "%" + this.frontPad(char.charCodeAt(0).toString(16).toUpperCase(), 2, "0");
.replace(/#/g, "%23") }
.replace(/'/g, "%27") return encoded;
.replace(/\(/g, "%28")
.replace(/\)/g, "%29")
.replace(/\*/g, "%2A")
.replace(/-/g, "%2D")
.replace(/\./g, "%2E")
.replace(/_/g, "%5F")
.replace(/~/g, "%7E");
} }
} }