Add warnings for invalid base64

Fixes #12.
This commit is contained in:
Florine W. Dekker 2022-11-23 17:43:14 +01:00
parent 451ee3b4b8
commit 1be17216b3
Signed by: FWDekker
GPG Key ID: D3DCFAA8A4560BE0
2 changed files with 28 additions and 13 deletions

View File

@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="Florine W. Dekker" />
<meta name="application-name" content="Converter" />
<meta name="description" content="Convert numbers to and from various bases." />
<meta name="description" content="Convert instantly between binary, decimal, hexadecimal, base64, and ASCII." />
<meta name="theme-color" content="#0033cc" />
<meta name="fwd:auto:show-main" />
@ -43,7 +43,7 @@
<h1><a href=".">Converter</a></h1>
<h2>
Convert numbers to and from various bases.
Separate values by commas to convert multiple at the same time.
Use commas to separate multiple values.
</h2>
</hgroup>
</header>

View File

@ -139,15 +139,28 @@ class NumeralSystemInput {
const label = stringToHtml(`<label for="b${base}-input">${this.name} <small>(base ${base})</small></label>`);
this.wrapper.appendChild(label);
this.textarea = stringToHtml(`<textarea id="b${base}-input" class="number-input"></textarea>`);
this.textarea = stringToHtml(`<textarea id="b${base}-input" class="number-input" rows="4"></textarea>`);
this.textarea.addEventListener("beforeinput", event => {
if (!this.numeralSystem.isLegal(event.data))
if (
(
event.data == null && event.inputType === "insertLineBreak" &&
!this.numeralSystem.alphabet.includes("\n")
) ||
(
event.data != null && !this.numeralSystem.isLegal(event.data)
)
) {
event.preventDefault();
}
});
this.textarea.addEventListener("input", () => {
if (this.textarea.value === "") return;
clearInputValidity(this.textarea);
updateAllInputs(this, this.numeralSystem.basesToDecimals(this.getValues()));
try {
updateAllInputs(this, this.numeralSystem.basesToDecimals(this.getValues()));
} catch (error) {
showInputInvalid(this.textarea, error.message);
}
});
this.wrapper.appendChild(this.textarea);
@ -180,6 +193,7 @@ class NumeralSystemInput {
* @param decimalNumbers {bigInt[]} the decimal numbers to represent in the input
*/
setValues(decimalNumbers) {
clearInputValidity(this.textarea);
this.textarea.value = this.numeralSystem.decimalsToBases(decimalNumbers).join(",");
}
}
@ -234,12 +248,14 @@ class Base64NumeralSystem extends NumeralSystem {
* @returns {bigInt} the decimal representation of `baseString`
*/
baseToDecimal(baseString) {
if (baseString.length % 4 === 1) throw new Error("Invalid input string length.");
const normalBaseString = stringReplaceAlls(baseString, this.alphabet, Base64NumeralSystem.defaultAlphabet());
const hex = Array.from(atob(normalBaseString))
.map(char => char.charCodeAt(0).toString(16).padStart(2, "0")).join("");
return bigInt(hex, 16);
try {
const normalBaseString = stringReplaceAlls(baseString, this.alphabet, Base64NumeralSystem.defaultAlphabet());
const hex = Array.from(atob(normalBaseString))
.map(char => char.charCodeAt(0).toString(16).padStart(2, "0")).join("");
return bigInt(hex, 16);
} catch (error) {
throw new Error("Invalid base64 string. Maybe the padding is wrong?");
}
}
}
@ -316,7 +332,6 @@ class NumeralSystemInputWithToggleableSeparator extends NumeralSystemInput {
console.warn("Toggleable separator input incorrectly used on numeral system without comma in alphabet.");
super(name, numeralSystem);
this.textarea.addEventListener("input", () => clearInputValidity(this.textarea));
const id = `b${this.numeralSystem.base}-separator-toggle`;
const checkboxContainer = document.createElement("div");