import {test, expect} from '../../fixtures/test' // editor.insertContent() inserts HTML at the current cursor position. // Canvas uses this to insert media embeds, file links, and equation images // from dialogs. Position matters: content must land where the cursor was, // not at the beginning/end, and existing content must not be displaced. test.describe('editor.insertContent() position and behavior', () => { test.beforeEach(async ({page, rcePage}) => { await page.goto('/scenarios/basic') await rcePage.waitForEditor() }) test('insertContent after placing cursor at end of paragraph', async ({page}) => { const content = await page.evaluate(() => { // @ts-expect-error -- TinyMCE global const ed = window.tinymce.activeEditor ed.setContent('
Existing text
') const p = ed.getBody().querySelector('p') ed.selection.setCursorLocation(p, p.childNodes.length) ed.insertContent(' inserted') return ed.getContent() }) expect(content).toContain('Existing text') expect(content).toContain('inserted') }) test('insertContent replaces selected text', async ({page}) => { const content = await page.evaluate(() => { // @ts-expect-error -- TinyMCE global const ed = window.tinymce.activeEditor ed.setContent('Replace this word please
') const p = ed.getBody().querySelector('p') // select all text in the paragraph ed.selection.select(p) ed.insertContent('New content') return ed.getContent() }) expect(content).toContain('New content') expect(content).not.toContain('Replace this word please') }) test('insertContent with an image tag — surrounding text preserved', async ({page}) => { const content = await page.evaluate(() => { // @ts-expect-error -- TinyMCE global const ed = window.tinymce.activeEditor ed.setContent('Before image
') const p = ed.getBody().querySelector('p') ed.selection.setCursorLocation(p, p.childNodes.length) ed.insertContent('
')
return ed.getContent()
})
expect(content).toContain('Before image')
expect(content).toContain('Inserted image')
})
test('insertContent with a table — table is present in output', async ({page}) => {
const content = await page.evaluate(() => {
// @ts-expect-error -- TinyMCE global
const ed = window.tinymce.activeEditor
ed.setContent('Before table
') const p = ed.getBody().querySelector('p') ed.selection.setCursorLocation(p, p.childNodes.length) ed.insertContent('| Inserted cell |
Start
') ed.insertContent(' First insert') ed.insertContent(' Second insert') return ed.getContent() }) expect(content).toContain('First insert') expect(content).toContain('Second insert') }) })