mirror of
https://github.com/misskey-dev/misskey.git
synced 2026-07-26 00:55:03 +02:00
* refactor(gh): CI用スクリプトをpackageとして整理 * fix * fix * fix * fix * fix * fix * fix * remove old scripts * migrate * refactor 1 * refactor 2 * fix comment * fix * fix * fix * fix * remove vite-node from changelog-checker * fix lint * fix * refactor * update deps * fix * spec: rename packages
64 lines
2.2 KiB
TypeScript
64 lines
2.2 KiB
TypeScript
/*
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
import { describe, expect, test } from 'vitest';
|
|
import { html, joinHtml, raw, Raw } from '../src/html';
|
|
|
|
describe('html', () => {
|
|
test('escapes interpolated values by default', () => {
|
|
const value = '<script>alert(1)</script>';
|
|
expect(String(html`<p>${value}</p>`)).toBe('<p><script>alert(1)</script></p>');
|
|
});
|
|
|
|
test('escapes values used in attribute position', () => {
|
|
const url = 'x"><script>alert(1)</script>';
|
|
expect(String(html`<a href="${url}"></a>`)).toBe('<a href="x"><script>alert(1)</script>"></a>');
|
|
});
|
|
|
|
test('leaves the static parts of the template untouched', () => {
|
|
expect(String(html`<p class="a">x</p>`)).toBe('<p class="a">x</p>');
|
|
});
|
|
|
|
test('renders nullish values as an empty string', () => {
|
|
expect(String(html`<p>${null}${undefined}</p>`)).toBe('<p></p>');
|
|
});
|
|
|
|
test('does not double-escape nested fragments', () => {
|
|
const inner = html`<b>${'a&b'}</b>`;
|
|
expect(String(html`<p>${inner}</p>`)).toBe('<p><b>a&b</b></p>');
|
|
});
|
|
|
|
// 配列をそのまま文字列化するとカンマ区切りで潰れる。実際に過去これで表が壊れた
|
|
test('joins arrays without separators instead of stringifying them', () => {
|
|
const items = [html`<li>1</li>`, html`<li>2</li>`];
|
|
expect(String(html`<ul>${items}</ul>`)).toBe('<ul><li>1</li><li>2</li></ul>');
|
|
});
|
|
|
|
test('escapes array elements that are not fragments', () => {
|
|
expect(String(html`<p>${['<a>', '<b>']}</p>`)).toBe('<p><a><b></p>');
|
|
});
|
|
});
|
|
|
|
describe('raw', () => {
|
|
test('embeds trusted markup without escaping', () => {
|
|
expect(String(html`<style>${raw('a > b { color: red }')}</style>`)).toBe('<style>a > b { color: red }</style>');
|
|
});
|
|
|
|
test('produces a Raw fragment', () => {
|
|
expect(raw('x')).toBeInstanceOf(Raw);
|
|
expect(html`x`).toBeInstanceOf(Raw);
|
|
});
|
|
});
|
|
|
|
describe('joinHtml', () => {
|
|
test('joins fragments with the given separator', () => {
|
|
expect(String(joinHtml([html`<li>1</li>`, html`<li>2</li>`], '\n'))).toBe('<li>1</li>\n<li>2</li>');
|
|
});
|
|
|
|
test('returns an empty fragment for an empty list', () => {
|
|
expect(String(joinHtml([], '\n'))).toBe('');
|
|
});
|
|
});
|