mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 10:18:38 +00:00
@@ -314,6 +314,7 @@ export function replaceTextareaSelection(textarea: HTMLTextAreaElement, text: st
|
||||
export function createElementFromHTML<T extends HTMLElement>(htmlString: string): T {
|
||||
htmlString = htmlString.trim();
|
||||
// some tags like "tr" are special, it must use a correct parent container to create
|
||||
// eslint-disable-next-line github/unescaped-html-literal -- FIXME: maybe we need to use other approaches to create elements from HTML, e.g. using DOMParser
|
||||
if (htmlString.startsWith('<tr')) {
|
||||
const container = document.createElement('table');
|
||||
container.innerHTML = htmlString;
|
||||
|
8
web_src/js/utils/html.test.ts
Normal file
8
web_src/js/utils/html.test.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import {html, htmlEscape, htmlRaw} from './html.ts';
|
||||
|
||||
test('html', async () => {
|
||||
expect(html`<a>${'<>&\'"'}</a>`).toBe(`<a><>&'"</a>`);
|
||||
expect(html`<a>${htmlRaw('<img>')}</a>`).toBe(`<a><img></a>`);
|
||||
expect(html`<a>${htmlRaw`<img ${'&'}>`}</a>`).toBe(`<a><img &></a>`);
|
||||
expect(htmlEscape(`<a></a>`)).toBe(`<a></a>`);
|
||||
});
|
32
web_src/js/utils/html.ts
Normal file
32
web_src/js/utils/html.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
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(parts[i]));
|
||||
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));
|
||||
}
|
Reference in New Issue
Block a user