Skip to content

Class: UtilsString ​

Utilities for manipulating and sanitizing strings: HTML escaping, anti-XSS cleanup and removal of accents/diacritics.

Remarks ​

Accessible everywhere via k.Utils.String. All methods are static.

Example ​

ts
// Safely display text entered by a viewer
element.innerHTML = k.Utils.String.htmlEscape(chatMessage);

Constructors ​

Constructor ​

ts
new UtilsString(): UtilsString;

Returns ​

UtilsString

Methods ​

htmlEscape() ​

ts
static htmlEscape(message): string;

Escapes the HTML of a string: special characters (<, >, &…) are converted to entities, so that the string is displayed as plain text without being interpreted.

Parameters ​

message ​

string

The message to escape.

Returns ​

string

The message with its HTML neutralized.

Remarks ​

Useful for inserting untrusted text into the DOM without any risk of injection.

Example ​

ts
k.Utils.String.htmlEscape('<b>hello</b>');
// → "&lt;b&gt;hello&lt;/b&gt;"

See ​

purify to allow safe HTML rather than escaping everything.


purify() ​

ts
static purify(message): string;

Sanitizes an HTML string via DOMPurify: keeps safe markup and removes anything that could be used for an XSS attack (scripts, on* handlers, etc.).

Parameters ​

message ​

string

The HTML string to clean.

Returns ​

string

The HTML stripped of its dangerous content.

Example ​

ts
k.Utils.String.purify('<img src=x onerror=alert(1)><b>ok</b>');
// → "<b>ok</b>" (the malicious attribute is removed)

See ​

htmlEscape to fully escape the HTML.


removeDiacritics() ​

ts
static removeDiacritics(string): string;

Removes the accents and diacritical marks from a string by replacing each accented character with its base letter (e.g. "é" → "e", "ç" → "c").

Parameters ​

string ​

string

The string to normalize.

Returns ​

string

The string without diacritics.

Remarks ​

Handy for comparing or matching chat commands regardless of accents.

Example ​

ts
k.Utils.String.removeDiacritics('Crème brûlée');
// → "Creme brulee"