import { Keyboard, Pipeline } from '@ephox/agar'; import { LegacyUnit, TinyDom, TinyLoader } from '@ephox/mcagar'; import Env from 'tinymce/core/api/Env'; import * as CaretContainer from 'tinymce/core/caret/CaretContainer'; import KeyUtils from '../module/test/KeyUtils'; import VK from 'tinymce/core/api/util/VK'; import Theme from 'tinymce/themes/modern/Theme'; import { UnitTest } from '@ephox/bedrock'; UnitTest.asynctest('browser.tinymce.core.SelectionOverridesTest', function () { const success = arguments[arguments.length - 2]; const failure = arguments[arguments.length - 1]; const suite = LegacyUnit.createSuite(); Theme(); const pressKey = function (key) { return function (editor) { Keyboard.keystroke(key, {}, TinyDom.fromDom(editor.getBody())); }; }; const exitPreTest = function (arrow, offset, expectedContent) { return function (editor) { editor.setContent('
abc'); LegacyUnit.setSelection(editor, 'pre', 1); arrow(editor); LegacyUnit.equal(editor.getContent(), '
abc'); LegacyUnit.equal(editor.selection.getNode().nodeName, 'PRE'); LegacyUnit.setSelection(editor, 'pre', offset); arrow(editor); LegacyUnit.equal(editor.getContent(), expectedContent); LegacyUnit.equal(editor.selection.getNode().nodeName, 'P'); }; }; const ok = function (a, label) { LegacyUnit.equal(a, true, label); }; const leftArrow = pressKey(VK.LEFT); const rightArrow = pressKey(VK.RIGHT); const upArrow = pressKey(VK.UP); const downArrow = pressKey(VK.DOWN); suite.test('left/right over cE=false inline', function (editor) { editor.focus(); editor.setContent('1'); editor.selection.select(editor.$('span')[0]); leftArrow(editor); LegacyUnit.equal(editor.getContent(), '
1
'); LegacyUnit.equal(CaretContainer.isCaretContainerInline(editor.selection.getRng().startContainer), true); LegacyUnit.equalDom(editor.selection.getRng().startContainer, editor.$('p')[0].firstChild); rightArrow(editor); LegacyUnit.equal(editor.getContent(), '1
'); LegacyUnit.equalDom(editor.selection.getNode(), editor.$('span')[0]); rightArrow(editor); LegacyUnit.equal(editor.getContent(), '1
'); LegacyUnit.equal(CaretContainer.isCaretContainerInline(editor.selection.getRng().startContainer), true); LegacyUnit.equalDom(editor.selection.getRng().startContainer, editor.$('p')[0].lastChild); }); suite.test('left/right over cE=false block', function (editor) { editor.setContent('1
'); editor.selection.select(editor.$('p')[0]); leftArrow(editor); LegacyUnit.equal(editor.getContent(), '1
'); LegacyUnit.equal(CaretContainer.isCaretContainerBlock(editor.selection.getRng().startContainer), true); rightArrow(editor); LegacyUnit.equal(editor.getContent(), '1
'); LegacyUnit.equalDom(editor.selection.getNode(), editor.$('p')[0]); rightArrow(editor); LegacyUnit.equal(editor.getContent(), '1
'); LegacyUnit.equal(CaretContainer.isCaretContainerBlock(editor.selection.getRng().startContainer), true); }); suite.test('left before cE=false block and type', function (editor) { editor.setContent('1
'); editor.selection.select(editor.$('p')[0]); leftArrow(editor); KeyUtils.type(editor, 'a'); LegacyUnit.equal(editor.getContent(), 'a
1
'); LegacyUnit.equal(CaretContainer.isCaretContainerBlock(editor.selection.getRng().startContainer.parentNode), false); }); suite.test('right after cE=false block and type', function (editor) { editor.setContent('1
'); editor.selection.select(editor.$('p')[0]); rightArrow(editor); KeyUtils.type(editor, 'a'); LegacyUnit.equal(editor.getContent(), '1
a
'); LegacyUnit.equal(CaretContainer.isCaretContainerBlock(editor.selection.getRng().startContainer.parentNode), false); }); suite.test('up from P to inline cE=false', function (editor) { editor.setContent('a1
abc
'); LegacyUnit.setSelection(editor, 'p:last', 3); upArrow(editor); LegacyUnit.equal(CaretContainer.isCaretContainerInline(editor.$('p:first')[0].lastChild), true); }); suite.test('down from P to inline cE=false', function (editor) { editor.setContent('abc
a1
'); LegacyUnit.setSelection(editor, 'p:first', 3); downArrow(editor); LegacyUnit.equal(CaretContainer.isCaretContainerInline(editor.$('p:last')[0].lastChild), true); }); suite.test('exit pre block (up)', exitPreTest(upArrow, 0, '\u00a0
abc')); suite.test('exit pre block (left)', exitPreTest(leftArrow, 0, '
\u00a0
abc')); suite.test('exit pre block (down)', exitPreTest(downArrow, 3, '
abc
\u00a0
')); suite.test('exit pre block (right)', exitPreTest(rightArrow, 3, 'abc
\u00a0
')); suite.test('click on link in cE=false', function (editor) { editor.setContent(''); const evt = editor.fire('click', { target: editor.$('strong')[0] }); LegacyUnit.equal(evt.isDefaultPrevented(), true); }); suite.test('click next to cE=false block', function (editor) { editor.setContent( '| 1 | ' + '2 | ' +
'
| 1 | 2 |
1
2
'); const rng = document.createRange(); rng.setStartBefore(editor.dom.select('p')[1]); rng.setEndBefore(editor.dom.select('p')[1]); editor.selection.setRng(rng, false); LegacyUnit.equal(editor.selection.getNode().getAttribute('data-mce-caret'), 'after'); }); suite.test('set range after ce=false element but lean forwards', function (editor) { editor.setContent('1
2
'); const rng = document.createRange(); rng.setStartBefore(editor.dom.select('p')[1]); rng.setEndBefore(editor.dom.select('p')[1]); editor.selection.setRng(rng, true); LegacyUnit.equal(editor.selection.getNode().getAttribute('data-mce-caret'), 'before'); }); suite.test('showCaret at TD', function (editor) { let rng; editor.setContent('| x |
| x |
|---|
a
b
'); rng = editor._selectionOverrides.showCaret(1, editor.dom.select('p')[0], true); LegacyUnit.equal(true, rng !== null, 'Should return a range'); editor._selectionOverrides.hideFakeCaret(); rng = editor._selectionOverrides.showCaret(1, editor.dom.select('p')[1], false); LegacyUnit.equal(true, rng === null, 'Should not return a range excluded by ShowCaret event'); editor._selectionOverrides.hideFakeCaret(); }); 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); });