/
githubmirror
/
marktext
Обзор
Документация
Войти
/
githubmirror
/
marktext
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
packages/muya/src/utils/__tests__/escapeHTML.spec.ts
38 строк
2 KB
Ran Luo
feat(muya): migrate TS rewrite to packages/muya alongside legacy muyajs (#4314)
29 май 2026, 15:35
Не верифицирован
29 май 2026, 15:35
0d86641
Код
Авторство
О чём код?
import { describe, expect, it } from 'vitest'; import { escapeHTML, unescapeHTML } from '../index'; // Lock in marktext fix dc54c7b6 (#2967, "escape html in code block"). The // rewritten implementation escapes `&` first (so existing `&` etc. // in the input survive a round-trip) and covers all five XML entities. describe('escapeHTML / unescapeHTML', () => { it('escapes all five entities', () => { expect(escapeHTML('<')).toBe('<'); expect(escapeHTML('>')).toBe('>'); expect(escapeHTML('"')).toBe('"'); expect(escapeHTML('\'')).toBe('''); expect(escapeHTML('&')).toBe('&'); }); it('escapes & first (pre-dc54c7b6 forgot &, which broke round-trips)', () => { // Pre-fix bug: escapeHtml('<') would return '<' unchanged // (no & escape), then unescapeHtml would turn it into '<' — // an information loss. The new impl encodes the leading & so // the original entity survives a round trip. const round = unescapeHTML(escapeHTML('<')); expect(round).toBe('<'); }); it('does not double-escape an already-escaped string round-tripped through unescape', () => { const malicious = '<script>alert("x")</script>'; const escaped = escapeHTML(malicious); expect(escaped).toBe( '<script>alert("x")</script>', ); expect(unescapeHTML(escaped)).toBe(malicious); }); it('preserves benign text untouched', () => { expect(escapeHTML('hello world')).toBe('hello world'); expect(escapeHTML('')).toBe(''); }); });