Autor: Zuby +++++ Datum: 28. 07. 2026 12:09:10
JtS »
// Hybrid encoding: ISO-8859-2 for Czech + HTML entities for emoji
function encodeFormValue(value: string): string {
const normalized = value.replace(/\r?\n/g, '\r\n');
let encoded = '';
for (const char of normalized) {
const mappedByte = ISO_8859_2_CHAR_TO_BYTE[char];
if (mappedByte !== undefined) {
// ISO-8859-2 characters: mapped byte value
const byte = mappedByte;
encoded += `%${byte.toString(16).toUpperCase().padStart(2, '0')}`;
} else if (char.charCodeAt(0) < 0x80) {
// ASCII - unreserved chars pass through, others are percent-encoded
const code = char.charCodeAt(0);
if (
(code >= 0x30 && code <= 0x39) || // 0-9
(code >= 0x41 && code <= 0x5A) || // A-Z
(code >= 0x61 && code <= 0x7A) || // a-z
code === 0x20 || // space
code === 0x2A || // *
code === 0x2D || // -
code === 0x2E || // .
code === 0x5F // _
) {
encoded += char === ' ' ? '+' : char;
} else {
encoded += `%${code.toString(16).toUpperCase().padStart(2, '0')}`;
}
} else {
// Emoji/Unicode: use HTML numeric entity encoding (codepointvalue;)
const codePoint = char.codePointAt(0) ?? 0x3F;
// Convert to HTML entity then percent-encode it
const entity = `${codePoint};`;
for (const entityChar of entity) {
const entityCode = entityChar.charCodeAt(0);
encoded += `%${entityCode.toString(16).toUpperCase().padStart(2, '0')}`;
}
}
}
return encoded;
} |
|