import { Pipeline } from '@ephox/agar'; import { LegacyUnit, TinyLoader } from '@ephox/mcagar'; import InsertContent from 'tinymce/core/InsertContent'; import Theme from 'tinymce/themes/modern/Theme'; import { UnitTest } from '@ephox/bedrock'; UnitTest.asynctest('browser.tinymce.core.InsertContentTest', function () { const success = arguments[arguments.length - 2]; const failure = arguments[arguments.length - 1]; const suite = LegacyUnit.createSuite(); Theme(); const assertSelection = function (editor, selector, offset) { const node = editor.$(selector)[0]; const rng = editor.selection.getRng(); LegacyUnit.equalDom(rng.startContainer, node.firstChild); LegacyUnit.equal(rng.startOffset, offset); LegacyUnit.equal(rng.collapsed, true); }; suite.test('insertAtCaret - i inside text, converts to em', function (editor) { editor.setContent('

1234

'); editor.focus(); LegacyUnit.setSelection(editor, 'p', 2); InsertContent.insertAtCaret(editor, 'a'); LegacyUnit.equal(editor.getContent(), '

12a34

'); }); suite.test('insertAtCaret - ul at beginning of li', function (editor) { editor.setContent(''); editor.focus(); LegacyUnit.setSelection(editor, 'li', 0); InsertContent.insertAtCaret(editor, { content: '', paste: true }); LegacyUnit.equal(editor.getContent(), ''); assertSelection(editor, 'li:nth-child(2)', 0); }); suite.test('insertAtCaret - ul with multiple items at beginning of li', function (editor) { editor.setContent(''); editor.focus(); LegacyUnit.setSelection(editor, 'li', 0); InsertContent.insertAtCaret(editor, { content: '', paste: true }); LegacyUnit.equal(editor.getContent(), ''); assertSelection(editor, 'li:nth-child(3)', 0); }); suite.test('insertAtCaret - ul at end of li', function (editor) { editor.setContent(''); editor.focus(); LegacyUnit.setSelection(editor, 'li', 2); InsertContent.insertAtCaret(editor, { content: '', paste: true }); LegacyUnit.equal(editor.getContent(), ''); assertSelection(editor, 'li:nth-child(2)', 1); }); suite.test('insertAtCaret - ul with multiple items at end of li', function (editor) { editor.setContent(''); editor.focus(); LegacyUnit.setSelection(editor, 'li', 2); InsertContent.insertAtCaret(editor, { content: '', paste: true }); LegacyUnit.equal(editor.getContent(), ''); assertSelection(editor, 'li:nth-child(4)', 1); }); suite.test('insertAtCaret - ul with multiple items in middle of li', function (editor) { editor.setContent(''); editor.focus(); LegacyUnit.setSelection(editor, 'li', 1); InsertContent.insertAtCaret(editor, { content: '', paste: true }); LegacyUnit.equal(editor.getContent(), ''); assertSelection(editor, 'li:nth-child(3)', 1); }); suite.test('insertAtCaret - ul in middle of li with formatting', function (editor) { editor.setContent(''); editor.focus(); LegacyUnit.setSelection(editor, 'strong', 1); InsertContent.insertAtCaret(editor, { content: '', paste: true }); LegacyUnit.equal( editor.getContent(), '' ); assertSelection(editor, 'li:nth-child(2)', 1); }); suite.test('insertAtCaret - ul with trailing empty block in middle of li', function (editor) { editor.setContent(''); editor.focus(); LegacyUnit.setSelection(editor, 'li:nth-child(1)', 1); InsertContent.insertAtCaret(editor, { content: '

\u00a0

', paste: true }); LegacyUnit.equal( editor.getContent(), '' ); assertSelection(editor, 'li:nth-child(3)', 1); }); suite.test('insertAtCaret - ul at beginning of li with empty end li', function (editor) { editor.setContent(''); editor.focus(); LegacyUnit.setSelection(editor, 'li', 0); InsertContent.insertAtCaret(editor, { content: '', paste: true }); LegacyUnit.equal(editor.getContent(), ''); assertSelection(editor, 'li:nth-child(2)', 0); }); suite.test('insertAtCaret - merge inline elements', function (editor) { editor.setContent('abc'); editor.focus(); LegacyUnit.setSelection(editor, 'em', 1); InsertContent.insertAtCaret(editor, { content: '123', merge: true }); LegacyUnit.equal(editor.getContent(), '

a123bc

'); }); suite.test('insertAtCaret - list into empty table cell with invalid contents #TINY-1231', function (editor) { editor.getBody().innerHTML = '

'; editor.focus(); const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('td')[0], 0); rng.setEnd(editor.dom.select('td')[0], 0); editor.selection.setRng(rng); InsertContent.insertAtCaret(editor, { content: '', paste: true }); LegacyUnit.equal(editor.getBody().innerHTML, '
  • a
'); assertSelection(editor, 'li', 1); }); suite.test('insertAtCaret - empty paragraph pad the empty element with br on insert and nbsp on save', function (editor) { editor.setContent('

ab

'); editor.focus(); LegacyUnit.setSelection(editor, 'p', 1); InsertContent.insertAtCaret(editor, { content: '

', merge: true }); LegacyUnit.equal(editor.getContent({ format: 'raw' }), '

a


b

'); LegacyUnit.equal(editor.getContent(), '

a

\u00a0

b

'); }); suite.test('insertAtCaret prevent default of beforeSetContent', function (editor) { let args; const handler = function (e) { if (e.selection === true) { e.preventDefault(); e.content = '

b

'; editor.getBody().innerHTML = '

c

'; } }; const collector = function (e) { args = e; }; editor.on('BeforeSetContent', handler); editor.on('SetContent', collector); editor.setContent('

a

'); LegacyUnit.setSelection(editor, 'p', 0); InsertContent.insertAtCaret(editor, { content: '

b

', paste: true }); LegacyUnit.equal(editor.getContent(), '

c

'); LegacyUnit.equal(args.content, '

b

'); LegacyUnit.equal(args.type, 'setcontent'); LegacyUnit.equal(args.paste, true); editor.off('BeforeSetContent', handler); editor.on('BeforeSetContent', collector); }); TinyLoader.setup(function (editor, onSuccess, onFailure) { Pipeline.async({}, suite.toSteps(editor), onSuccess, onFailure); }, { selector: 'textarea', add_unload_trigger: false, disable_nodechange: true, entities: 'raw', indent: false, skin_url: '/project/js/tinymce/skins/lightgray' }, success, failure); });