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('

link

'); 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
' ); const firstTd = editor.dom.select('td')[0]; const rect = editor.dom.getRect(firstTd); editor.fire('mousedown', { target: firstTd, clientX: rect.x + rect.w, clientY: rect.y + 10 }); // Since we can't do a real click we need to check if it gets sucked in towards the cE=false block LegacyUnit.equal(editor.selection.getNode().nodeName !== 'P', true); }); suite.test('offscreen copy of cE=false block remains offscreen', function (editor) { if (Env.ie || Env.gecko) { editor.setContent( '' + '' + '
12
' ); editor.selection.select(editor.dom.select('table')[0]); const offscreenSelection = editor.dom.select('.mce-offscreen-selection')[0]; ok(offscreenSelection.offsetLeft !== undefined, 'The offscreen selection\'s left border is undefined'); ok(offscreenSelection.offsetLeft < 0, 'The offscreen selection\'s left border is onscreen'); ok(offscreenSelection.offsetWidth + offscreenSelection.offsetLeft < 0, 'The cE=false offscreen selection is visible on-screen. Right edge: ' + offscreenSelection.offsetLeft + '+' + offscreenSelection.offsetWidth + '=' + (offscreenSelection.offsetLeft + offscreenSelection.offsetWidth) + 'px' ); } else { // Chrome and Safari behave correctly, and PhantomJS also declares itself as WebKit but does not // put the off-screen selection off-screen, so fails the above tests. However, it has no visible UI, // so everything is off-screen anyway :-) ok(true, 'Not a tested browser - Chrome & Safari work, PhantomJS does not put the selection off screen'); } }); suite.test('set range after ce=false element but lean backwards', 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, 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
'); rng = editor._selectionOverrides.showCaret(1, editor.dom.select('td')[0], true); LegacyUnit.equal(true, rng === null, 'Should be null since TD is not a valid caret target'); }); suite.test('showCaret at TH', function (editor) { let rng; editor.setContent('
x
'); rng = editor._selectionOverrides.showCaret(1, editor.dom.select('th')[0], true); LegacyUnit.equal(true, rng === null, 'Should be null since TH is not a valid caret target'); }); suite.test('showCaret block on specific element', function (editor) { let rng; editor.on('ShowCaret', function (e) { if (e.target.getAttribute('data-no-cef') === 'true') { e.preventDefault(); } }); editor.setContent('

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); });