Files

50 lines
1.6 KiB
TypeScript

/**
* jsdom lacks parts of the CSSOM. Reka's Presence uses `CSS.escape` when it
* resolves exiting children, so tests that dispatch real `animationend` events
* need the same capability a browser provides. Only fills the gap; never
* overrides an existing implementation.
*/
type CssEscapeHost = { escape?: (value: string) => string };
const cssGlobal = globalThis as unknown as { CSS?: CssEscapeHost };
if (!cssGlobal.CSS) cssGlobal.CSS = {};
if (typeof cssGlobal.CSS.escape !== "function") {
// CSSOM identifier serialization, including leading digits after a minus,
// control characters and the otherwise-invalid single minus identifier.
const escapeIdentifier = (value: string) => {
const input = String(value);
let output = "";
for (let index = 0; index < input.length; index += 1) {
const char = input[index];
const code = input.charCodeAt(index);
if (code === 0) {
output += "\uFFFD";
} else if (
(code >= 1 && code <= 0x1f) ||
code === 0x7f ||
(code >= 0x30 &&
code <= 0x39 &&
(index === 0 || (index === 1 && input[0] === "-")))
) {
output += `\\${code.toString(16)} `;
} else if (index === 0 && code === 0x2d && input.length === 1) {
output += "\\-";
} else if (
code >= 0x80 ||
code === 0x2d ||
code === 0x5f ||
(code >= 0x30 && code <= 0x39) ||
(code >= 0x41 && code <= 0x5a) ||
(code >= 0x61 && code <= 0x7a)
) {
output += char;
} else {
output += `\\${char}`;
}
}
return output;
};
cssGlobal.CSS.escape = escapeIdentifier;
}