import { formatHTML } from 'diffable-html-snapshot' import { describe, expect, it } from 'vitest' import { setupTest } from '../testing/index.ts' describe('defineFontFamilySpec', () => { it('should render font-family as inline span with style attribute', () => { const { editor, n, m } = setupTest() const doc = n.doc( n.p( 'Default', m.fontFamily({ family: 'Arial' }, 'arial'), m.fontFamily({ family: 'Times New Roman' }, 'times'), m.fontFamily({ family: 'Courier New, monospace' }, 'monospace'), ), ) editor.set(doc) expect(formatHTML(editor.view.dom.innerHTML)).toMatchInlineSnapshot( ` "
Default arial times monospace
" `, ) }) it('should parse font-family from style attribute', () => { const { editor } = setupTest() const html = `text
` editor.setContent(html) expect(editor.state.doc.firstChild?.firstChild?.toJSON()).toMatchInlineSnapshot(` { "marks": [ { "attrs": { "family": "Arial", }, "type": "fontFamily", }, ], "text": "text", "type": "text", } `) }) it('should parse font-family from data-font-family attribute', () => { const { editor } = setupTest() const html = `text
` editor.setContent(html) expect(editor.state.doc.firstChild?.firstChild?.toJSON()).toMatchInlineSnapshot(` { "marks": [ { "attrs": { "family": "Times New Roman", }, "type": "fontFamily", }, ], "text": "text", "type": "text", } `) }) it('should prioritize data-font-family attribute over style attribute', () => { const { editor } = setupTest() const html = `This should be Arial
` editor.setContent(html) expect(editor.state.doc.firstChild?.firstChild?.toJSON()).toMatchInlineSnapshot(` { "marks": [ { "attrs": { "family": "Arial", }, "type": "fontFamily", }, ], "text": "This should be Arial", "type": "text", } `) }) it('can handle non-span inline elements', () => { const { editor } = setupTest() const html = `` + `This should be Arial and italic` + `This should be Georgia and italic` + `This should be Courier New and bold` + `
` editor.setContent(html) expect(editor.state.doc.firstChild?.toJSON()).toMatchInlineSnapshot(` { "content": [ { "marks": [ { "attrs": { "family": "Arial", }, "type": "fontFamily", }, { "type": "italic", }, ], "text": "This should be Arial and italic", "type": "text", }, { "marks": [ { "attrs": { "family": "Georgia", }, "type": "fontFamily", }, { "type": "italic", }, ], "text": "This should be Georgia and italic", "type": "text", }, { "marks": [ { "attrs": { "family": "\"Courier New\"", }, "type": "fontFamily", }, { "type": "bold", }, ], "text": "This should be Courier New and bold", "type": "text", }, ], "type": "paragraph", } `) }) it('should ignore empty attributes', () => { const { editor } = setupTest() const html = `This should be plain text
` editor.setContent(html) expect(editor.state.doc.firstChild?.firstChild?.toJSON()).toMatchInlineSnapshot(` { "text": "This should be plain text", "type": "text", } `) }) it('should ignore inherit attributes', () => { const { editor } = setupTest() const html = `This should be plain text
` editor.setContent(html) expect(editor.state.doc.firstChild?.firstChild?.toJSON()).toMatchInlineSnapshot(` { "text": "This should be plain text", "type": "text", } `) }) })