@tiptap/core
Version:
headless rich text editor
56 lines (43 loc) • 1.7 kB
text/typescript
import { describe, expect, it } from 'vitest'
import { decodeHtmlEntities, encodeHtmlEntities } from '../utilities/htmlEntities.js'
describe('decodeHtmlEntities', () => {
it('decodes < to <', () => {
expect(decodeHtmlEntities('<div>')).toBe('<div>')
})
it('decodes & to &', () => {
expect(decodeHtmlEntities('a & b')).toBe('a & b')
})
it('decodes " to "', () => {
expect(decodeHtmlEntities('"hello"')).toBe('"hello"')
})
it('handles doubly-encoded sequences like &lt;', () => {
expect(decodeHtmlEntities('&lt;')).toBe('<')
})
it('returns plain text unchanged', () => {
expect(decodeHtmlEntities('hello world')).toBe('hello world')
})
})
describe('encodeHtmlEntities', () => {
it('encodes < to <', () => {
expect(encodeHtmlEntities('<div>')).toBe('<div>')
})
it('encodes & to &', () => {
expect(encodeHtmlEntities('a & b')).toBe('a & b')
})
it('does not encode " (quotes are valid in markdown)', () => {
expect(encodeHtmlEntities('"hello"')).toBe('"hello"')
})
it('returns plain text unchanged', () => {
expect(encodeHtmlEntities('hello world')).toBe('hello world')
})
})
describe('roundtrip', () => {
it.each(['<div>', 'a & b', 'x < y & y > z'])('encode then decode roundtrips: %s', input => {
expect(decodeHtmlEntities(encodeHtmlEntities(input))).toBe(input)
})
it('decode is a superset of encode – " decodes but " is not encoded', () => {
// " passes through encode unchanged, " decodes to "
expect(encodeHtmlEntities('"hello"')).toBe('"hello"')
expect(decodeHtmlEntities('"hello"')).toBe('"hello"')
})
})