import { describe, it } from '@ephox/bedrock-client'; import { Hierarchy } from '@ephox/sugar'; import { TinyAssertions, TinyDom, TinyHooks, TinySelections } from '@ephox/wrap-mcagar'; import { assert } from 'chai'; import Editor from 'tinymce/core/api/Editor'; import * as DeleteElement from 'tinymce/core/delete/DeleteElement'; describe('browser.tinymce.core.delete.DeleteElementTest', () => { const hook = TinyHooks.bddSetupLight({ add_unload_trigger: false, indent: false, base_url: '/project/tinymce/js/tinymce' }, [], true); const deleteElementPath = (editor: Editor, forward: boolean, path: number[]) => { const element = Hierarchy.follow(TinyDom.body(editor), path).getOrDie(); DeleteElement.deleteElement(editor, forward, element); }; const assertCaretDirection = (editor: Editor, expectedCaretData: 'before' | 'after') => { assert.equal(editor.selection.getNode().getAttribute('data-mce-caret'), expectedCaretData, 'Should have the right caret data'); }; it('Delete image forwards', () => { const editor = hook.editor(); editor.setContent('

'); TinySelections.setCursor(editor, [ 0 ], 0); deleteElementPath(editor, true, [ 0, 0 ]); TinyAssertions.assertContent(editor, ''); TinyAssertions.assertSelection(editor, [ 0 ], 0, [ 0 ], 0); }); it('Delete image backwards', () => { const editor = hook.editor(); editor.setContent('

'); TinySelections.setCursor(editor, [ 0 ], 1); deleteElementPath(editor, false, [ 0, 0 ]); TinyAssertions.assertContent(editor, ''); TinyAssertions.assertSelection(editor, [ 0 ], 0, [ 0 ], 0); }); it('Delete first image forwards caret before', () => { const editor = hook.editor(); editor.setContent('

'); TinySelections.setCursor(editor, [ 0 ], 0); deleteElementPath(editor, true, [ 0, 0 ]); TinyAssertions.assertContent(editor, '

'); TinyAssertions.assertSelection(editor, [ 0 ], 0, [ 0 ], 0); }); it('Delete first image forwards caret after', () => { const editor = hook.editor(); editor.setContent('

'); TinySelections.setCursor(editor, [ 0 ], 1); deleteElementPath(editor, true, [ 0, 0 ]); TinyAssertions.assertContent(editor, '

'); TinyAssertions.assertSelection(editor, [ 0 ], 0, [ 0 ], 0); }); it('Delete first image backwards', () => { const editor = hook.editor(); editor.setContent('

'); TinySelections.setCursor(editor, [ 0 ], 2); deleteElementPath(editor, false, [ 0, 0 ]); TinyAssertions.assertContent(editor, '

'); TinyAssertions.assertSelection(editor, [ 0 ], 0, [ 0 ], 0); }); it('Delete second image forwards caret before', () => { const editor = hook.editor(); editor.setContent('

'); TinySelections.setCursor(editor, [ 0 ], 1); deleteElementPath(editor, true, [ 0, 1 ]); TinyAssertions.assertContent(editor, '

'); TinyAssertions.assertSelection(editor, [ 0 ], 1, [ 0 ], 1); }); it('Delete second image forwards caret after', () => { const editor = hook.editor(); editor.setContent('

'); TinySelections.setCursor(editor, [ 0 ], 2); deleteElementPath(editor, true, [ 0, 1 ]); TinyAssertions.assertContent(editor, '

'); TinyAssertions.assertSelection(editor, [ 0 ], 1, [ 0 ], 1); }); it('Delete second image backwards caret before', () => { const editor = hook.editor(); editor.setContent('

'); TinySelections.setCursor(editor, [ 0 ], 1); deleteElementPath(editor, false, [ 0, 1 ]); TinyAssertions.assertContent(editor, '

'); TinyAssertions.assertSelection(editor, [ 0 ], 1, [ 0 ], 1); }); it('Delete second image backwards caret after', () => { const editor = hook.editor(); editor.setContent('

'); TinySelections.setCursor(editor, [ 0 ], 2); deleteElementPath(editor, false, [ 0, 1 ]); TinyAssertions.assertContent(editor, '

'); TinyAssertions.assertSelection(editor, [ 0 ], 1, [ 0 ], 1); }); it('Delete forwards on paragraph to next paragraph with caret position (text)', () => { const editor = hook.editor(); editor.setContent('

a

b

'); TinySelections.setCursor(editor, [ 0, 0 ], 1); deleteElementPath(editor, true, [ 0 ]); TinyAssertions.assertContent(editor, '

b

'); TinyAssertions.assertSelection(editor, [ 0, 0 ], 0, [ 0, 0 ], 0); }); it('Delete backwards on paragraph to previous paragraph with caret position (text)', () => { const editor = hook.editor(); editor.setContent('

a

b

'); TinySelections.setCursor(editor, [ 1, 0 ], 0); deleteElementPath(editor, false, [ 1 ]); TinyAssertions.assertContent(editor, '

a

'); TinyAssertions.assertSelection(editor, [ 0, 0 ], 1, [ 0, 0 ], 1); }); it('Delete forwards on paragraph to previous paragraph with caret position (text)', () => { const editor = hook.editor(); editor.setContent('

a

b

'); TinySelections.setCursor(editor, [ 1, 0 ], 1); deleteElementPath(editor, true, [ 1 ]); TinyAssertions.assertContent(editor, '

a

'); TinyAssertions.assertSelection(editor, [ 0, 0 ], 1, [ 0, 0 ], 1); }); it('Delete backwards on paragraph to next paragraph with caret position (text)', () => { const editor = hook.editor(); editor.setContent('

a

b

'); TinySelections.setCursor(editor, [ 0, 0 ], 0); deleteElementPath(editor, false, [ 0 ]); TinyAssertions.assertContent(editor, '

b

'); TinyAssertions.assertSelection(editor, [ 0, 0 ], 0, [ 0, 0 ], 0); }); it('Delete forwards paragraph before paragraph with caret position (element)', () => { const editor = hook.editor(); editor.setContent('

'); TinySelections.setCursor(editor, [ 0 ], 1); deleteElementPath(editor, true, [ 0 ]); TinyAssertions.assertContent(editor, '

'); TinyAssertions.assertSelection(editor, [ 0 ], 0, [ 0 ], 0); }); it('Delete backwards paragraph after paragraph with caret position (element)', () => { const editor = hook.editor(); editor.setContent('

'); TinySelections.setCursor(editor, [ 1 ], 0); deleteElementPath(editor, false, [ 0 ]); TinyAssertions.assertContent(editor, '

'); TinyAssertions.assertSelection(editor, [ 0 ], 0, [ 0 ], 0); }); it('Delete backwards on cef block between cef blocks', () => { const editor = hook.editor(); editor.setContent('

a

b

c

'); TinySelections.setSelection(editor, [], 1, [], 2); deleteElementPath(editor, false, [ 1 ]); TinyAssertions.assertContent(editor, '

a

c

'); TinyAssertions.assertSelection(editor, [ 1 ], 0, [ 1 ], 0); assertCaretDirection(editor, 'after'); }); it('Delete forwards on cef block between cef blocks', () => { const editor = hook.editor(); editor.setContent('

a

b

c

'); TinySelections.setSelection(editor, [], 1, [], 2); deleteElementPath(editor, true, [ 1 ]); TinyAssertions.assertContent(editor, '

a

c

'); TinyAssertions.assertSelection(editor, [ 1 ], 0, [ 1 ], 0); assertCaretDirection(editor, 'before'); }); it('Delete element adjacent text nodes forward', () => { const editor = hook.editor(); editor.setContent('

a
b

'); TinySelections.setCursor(editor, [ 0, 0 ], 1); deleteElementPath(editor, true, [ 0, 1 ]); TinyAssertions.assertContent(editor, '

ab

'); TinyAssertions.assertSelection(editor, [ 0, 0 ], 1, [ 0, 0 ], 1); }); it('Delete element adjacent text nodes backwards', () => { const editor = hook.editor(); editor.setContent('

a
b

'); TinySelections.setCursor(editor, [ 0, 2 ], 0); deleteElementPath(editor, false, [ 0, 1 ]); TinyAssertions.assertContent(editor, '

ab

'); TinyAssertions.assertSelection(editor, [ 0, 0 ], 1, [ 0, 0 ], 1); }); it('Delete inline element adjacent text nodes forwards', () => { const editor = hook.editor(); editor.setContent('

a b c

'); TinySelections.setCursor(editor, [ 0, 0 ], 2); deleteElementPath(editor, true, [ 0, 1 ]); TinyAssertions.assertContent(editor, '

a  c

'); TinyAssertions.assertSelection(editor, [ 0, 0 ], 2, [ 0, 0 ], 2); }); it('Delete inline element adjacent text nodes backwards', () => { const editor = hook.editor(); editor.setContent('

b  c

'); TinySelections.setCursor(editor, [ 0, 2 ], 0); deleteElementPath(editor, false, [ 0, 1 ]); TinyAssertions.assertContent(editor, '

a    c

'); TinyAssertions.assertSelection(editor, [ 0, 0 ], 3, [ 0, 0 ], 3); }); it('Delete inline element adjacent text nodes, single space', () => { const editor = hook.editor(); editor.setContent('

a bc

'); TinySelections.setCursor(editor, [ 0, 1 ], 0); deleteElementPath(editor, false, [ 0, 1 ]); TinyAssertions.assertContent(editor, '

a c

'); TinyAssertions.assertSelection(editor, [ 0, 0 ], 2, [ 0, 0 ], 2); }); it('Delete inline element leading only text nodes', () => { const editor = hook.editor(); editor.setContent('

a b

'); TinySelections.setCursor(editor, [ 0, 1 ], 0); deleteElementPath(editor, false, [ 0, 1 ]); TinyAssertions.assertContent(editor, '

'); TinyAssertions.assertSelection(editor, [ 0, 0 ], 2, [ 0, 0 ], 2); }); it('Delete inline element trailing only text nodes', () => { const editor = hook.editor(); editor.setContent('

a b

'); TinySelections.setCursor(editor, [ 0, 1 ], 0); deleteElementPath(editor, false, [ 0, 0 ]); TinyAssertions.assertContent(editor, '

 b

'); TinyAssertions.assertSelection(editor, [ 0, 0 ], 0, [ 0, 0 ], 0); }); });