mirror of
https://github.com/go-gitea/gitea.git
synced 2025-09-18 23:48:18 +02:00
Update eslint and all plugins. Many plugins still do not ship type definitions so I had to add stubs. Also, I had to put a few typescript error expectations because if some unknown error in the types. `eslint-plugin-no-jquery` is disabled because it's not compatible with eslint 9 flat config (https://github.com/wikimedia/eslint-plugin-no-jquery/issues/311).
33 lines
1.1 KiB
TypeScript
33 lines
1.1 KiB
TypeScript
export function htmlEscape(s: string, ...args: Array<any>): string {
|
|
if (args.length !== 0) throw new Error('use html or htmlRaw instead of htmlEscape'); // check legacy usages
|
|
return s.replace(/&/g, '&')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, ''')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>');
|
|
}
|
|
|
|
class rawObject {
|
|
private readonly value: string;
|
|
constructor(v: string) { this.value = v }
|
|
toString(): string { return this.value }
|
|
}
|
|
|
|
export function html(tmpl: TemplateStringsArray, ...parts: Array<any>): string {
|
|
let output = tmpl[0];
|
|
for (let i = 0; i < parts.length; i++) {
|
|
const value = parts[i];
|
|
const valueEscaped = (value instanceof rawObject) ? value.toString() : htmlEscape(String(value));
|
|
output = output + valueEscaped + tmpl[i + 1];
|
|
}
|
|
return output;
|
|
}
|
|
|
|
export function htmlRaw(s: string | TemplateStringsArray, ...tmplParts: Array<any>): rawObject {
|
|
if (typeof s === 'string') {
|
|
if (tmplParts.length !== 0) throw new Error("either htmlRaw('str') or htmlRaw`tmpl`");
|
|
return new rawObject(s);
|
|
}
|
|
return new rawObject(html(s, ...tmplParts));
|
|
}
|