'use strict'; /* * Plain-text hygiene for everything players type that other players read (messages, notes, alliance names). * Strips control characters and the invisible/bidirectional formatting characters that can be used to spoof text * ("Trojan Source" style reordering, zero-width padding to hide content), normalises to NFC and fixes lone surrogates. * Newlines and tabs survive only in multiline text. */ // C0 (except \t \n, CR is normalised first), DEL + C1, ALM, zero-width space, LRM/RLM, bidi embeddings/overrides // (202A-202E), word joiner and invisible operators (2060-2064), bidi isolates and deprecated formatting (2066-206F), // BOM, interlinear annotation, and the invisible Unicode tag block. ZWJ/ZWNJ (200C/200D) stay: emoji and some scripts need them. const STRIP = new RegExp('[\\u0000-\\u0008\\u000B-\\u001F\\u007F-\\u009F\\u061C\\u200B\\u200E\\u200F\\u202A-\\u202E\\u2060-\\u2064\\u2066-\\u206F\\uFEFF\\uFFF9-\\uFFFB\\u{E0000}-\\u{E007F}]', 'gu'); function cleanText(input, opts) { const multiline = !!(opts && opts.multiline); let s = String(input === undefined || input === null ? '' : input); if (s.toWellFormed) s = s.toWellFormed(); s = s.normalize('NFC').replace(/\r\n?/g, '\n').replace(/[\u2028\u2029\u0085]/g, '\n').replace(STRIP, ''); if (multiline) s = s.replace(/[ \t]+\n/g, '\n').replace(/\n{4,}/g, '\n\n\n'); else s = s.replace(/[\n\t]+/g, ' ').replace(/ {2,}/g, ' '); return s.trim(); } // Length in code points (what a player counts), not UTF-16 units. const textLength = (s) => { let n = 0; for (const _ of s) n++; return n; }; // eslint-disable-line no-unused-vars module.exports = { cleanText, textLength };