import { ApproxStructure, Assertions, GeneralSteps, Keys, Logger, Pipeline, Step } from '@ephox/agar'; import { Fun } from '@ephox/katamari'; import { TinyActions, TinyApis, TinyLoader } from '@ephox/mcagar'; import CaretPosition from 'tinymce/core/caret/CaretPosition'; import BoundaryLocation from 'tinymce/core/keyboard/BoundaryLocation'; import InlineUtils from 'tinymce/core/keyboard/InlineUtils'; import Theme from 'tinymce/themes/modern/Theme'; import { UnitTest } from '@ephox/bedrock'; UnitTest.asynctest('browser.tinymce.core.delete.InlineBoundaryDeleteTest', function () { const success = arguments[arguments.length - 2]; const failure = arguments[arguments.length - 1]; Theme(); const locationName = function (location) { return location.fold( Fun.constant('before'), Fun.constant('start'), Fun.constant('end'), Fun.constant('after') ); }; const readLocation = function (editor) { const isInlineTarget = Fun.curry(InlineUtils.isInlineTarget, editor); return BoundaryLocation .readLocation(isInlineTarget, editor.getBody(), CaretPosition.fromRangeStart(editor.selection.getRng())) .map(locationName) .getOr('none'); }; const sTestDeleteOrBackspaceKey = function (editor, tinyApis, tinyActions, key) { return function (setupHtml, setupPath, setupOffset, expectedHtml, expectedLocation, expectedPath, expectedOffet) { return GeneralSteps.sequence([ tinyApis.sSetContent(setupHtml), tinyApis.sSetCursor(setupPath, setupOffset), tinyApis.sNodeChanged, tinyActions.sContentKeystroke(key, { }), tinyApis.sAssertContent(expectedHtml), Step.sync(function () { Assertions.assertEq('Should be expected location', expectedLocation, readLocation(editor)); }), tinyApis.sAssertSelection(expectedPath, expectedOffet, expectedPath, expectedOffet), sNormalizeBody(editor) ]); }; }; const sNormalizeBody = function (editor) { return Step.sync(function () { editor.getBody().normalize(); }); }; const paragraphWithText = function (text) { return ApproxStructure.build(function (s, str, arr) { return s.element('body', { children: [s.element('p', { children: [s.text(str.is(text))] })] }); }); }; TinyLoader.setup(function (editor, onSuccess, onFailure) { const tinyApis = TinyApis(editor); const tinyActions = TinyActions(editor); const sTestBackspace = sTestDeleteOrBackspaceKey(editor, tinyApis, tinyActions, Keys.backspace()); const sTestDelete = sTestDeleteOrBackspaceKey(editor, tinyApis, tinyActions, 46); Pipeline.async({}, [ tinyApis.sFocus, Logger.t('Backspace key on text', GeneralSteps.sequence([ sTestBackspace('

abc

', [0, 2], 0, '

abc

', 'end', [0, 1, 0], 1), sTestBackspace('

abc

', [0, 1, 0], 0, '

abc

', 'before', [0, 0], 1), sTestBackspace('

abcd

', [0, 1, 0], 1, '

acd

', 'start', [0, 1, 0], 1) ])), Logger.t('Backspace key on image', GeneralSteps.sequence([ sTestBackspace('

ac

', [0, 2], 0, '

ac

', 'end', [0, 1, 1], 0), sTestBackspace('

ac

', [0, 1], 0, '

ac

', 'before', [0, 0], 1), tinyApis.sExecCommand('SelectAll'), // Needed for IE 11 for some odd reason the selection api is in some odd state sTestBackspace('

acd

', [0, 1], 1, '

acd

', 'start', [0, 1, 0], 1) ])), Logger.t('Delete key on text', GeneralSteps.sequence([ sTestDelete('

abc

', [0, 0], 1, '

abc

', 'start', [0, 1, 0], 1), sTestDelete('

abc

', [0, 1, 0], 1, '

abc

', 'after', [0, 2], 1), sTestDelete('

abcd

', [0, 1, 0], 1, '

abd

', 'end', [0, 1, 0], 1) ])), Logger.t('Delete key on image', GeneralSteps.sequence([ sTestDelete('

ac

', [0, 0], 1, '

ac

', 'start', [0, 1, 0], 1), sTestDelete('

ac

', [0, 1], 1, '

ac

', 'after', [0, 2], 1), sTestDelete('

abd

', [0, 1, 0], 1, '

abd

', 'end', [0, 1, 0], 1) ])), Logger.t('Backspace/delete last character', GeneralSteps.sequence([ sTestDelete('

abc

', [0, 1, 0], 0, '

ac

', 'none', [0, 0], 1), sTestDelete('

b

', [0, 1, 0], 0, '

', 'none', [0], 1), sTestDelete('

abc

', [0, 1, 0], 0, '

ac

', 'none', [0, 0], 1), tinyApis.sAssertContentStructure(paragraphWithText('ac')), sTestBackspace('

abc

', [0, 1, 0], 1, '

ac

', 'none', [0, 0], 1), tinyApis.sAssertContentStructure(paragraphWithText('ac')), sTestDelete('

ac

', [0, 1], 0, '

ac

', 'none', [0, 0], 1), sTestBackspace('

ac

', [0, 1], 1, '

ac

', 'none', [0, 0], 1) ])), Logger.t('Backspace/delete between blocks', GeneralSteps.sequence([ sTestBackspace('

a

b

', [1], 0, '

ab

', 'end', [0, 0, 0], 1), sTestDelete('

a

b

', [0], 1, '

ab

', 'end', [0, 0, 0], 1) ])), Logger.t('Backspace key inline_boundaries: false', GeneralSteps.sequence([ tinyApis.sSetSetting('inline_boundaries', false), sTestBackspace('

abc

', [0, 2], 0, '

abc

', 'after', [0, 2], 0), tinyApis.sSetSetting('inline_boundaries', true) ])) ], onSuccess, onFailure); }, { skin_url: '/project/js/tinymce/skins/lightgray' }, success, failure); });