Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
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));
|
|
}
|