Upstream/server/text.js
jvanvik 249818a7aa Stage 3-5 server (military, expansion, social), web client for stages 1-2, ops/deploy
Built: units/research/training, movements (scan, raid, attack, takeover, reinforce, settle, transport), deterministic
combat resolved in the event queue, reports, power/offline units, founding + conquest + incumbent sites; alliances,
roles, diplomacy (24h pact cancel via events), messages, rankings; vanilla-JS phone-first client (auth, POP, map,
alerts, settings, en/nb); systemd units, backup timer, DEPLOY.md, tools/admin.js.
Tested: npm test 96/96 (fake clock, in-memory db, a few real-HTTP tests); client checked with headless Chromium
screenshots only. NOT done: marketplace/transport trade, client screens for military/social, WebSocket push,
bots/balance, e2e suite. All unit stats and costs are unbalanced first guesses.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
2026-10-11 18:32:36 +02:00

26 lines
1.6 KiB
JavaScript

'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 };