import { Pipeline } from '@ephox/agar'; import { LegacyUnit, TinyLoader } from '@ephox/mcagar'; import Env from 'tinymce/core/api/Env'; import HtmlUtils from '../../module/test/HtmlUtils'; import Tools from 'tinymce/core/api/util/Tools'; import Theme from 'tinymce/themes/modern/Theme'; import { UnitTest } from '@ephox/bedrock'; UnitTest.asynctest('browser.tinymce.core.keyboard.EnterKey', function () { const success = arguments[arguments.length - 2]; const failure = arguments[arguments.length - 1]; const suite = LegacyUnit.createSuite(); Theme(); const pressEnter = function (editor, evt?) { const dom = editor.dom, target = editor.selection.getNode(); evt = Tools.extend({ keyCode: 13, shiftKey: false }, evt); dom.fire(target, 'keydown', evt); dom.fire(target, 'keypress', evt); dom.fire(target, 'keyup', evt); }; suite.test('Enter at end of H1', function (editor) { editor.setContent('
\u00a0
'); LegacyUnit.equal(editor.selection.getRng(true).startContainer.nodeName, 'P'); }); suite.test('Enter in midde of H1', function (editor) { editor.setContent('ab
'); editor.selection.setCursorLocation(editor.getBody().firstChild, 1); pressEnter(editor); LegacyUnit.equal(editor.getContent(), 'a
b
'); const rng = editor.selection.getRng(true); LegacyUnit.equal(rng.startContainer.nodeValue, 'b'); }); suite.test('Enter before first IMG in P', function (editor) { editor.setContent('\u00a0
\u00a0
abc
abc
abc
abc
\u00a0
'); }); suite.test('Enter before last INPUT in P with text', function (editor) { editor.setContent('abc
'); editor.selection.setCursorLocation(editor.getBody().firstChild, 1); pressEnter(editor); LegacyUnit.equal(editor.getContent(), 'abc
'); const rng = editor.selection.getRng(true); LegacyUnit.equal(rng.startContainer.nodeName, 'P'); LegacyUnit.equal(rng.startContainer.childNodes[rng.startOffset].nodeName, 'INPUT'); }); suite.test('Enter before last INPUT in P with IMG sibling', function (editor) { editor.setContent(''); editor.selection.setCursorLocation(editor.getBody().firstChild, 1); pressEnter(editor); LegacyUnit.equal(editor.getContent(), ''); const rng = editor.selection.getRng(true); LegacyUnit.equal(rng.startContainer.nodeName, 'P'); LegacyUnit.equal(rng.startContainer.childNodes[rng.startOffset].nodeName, 'INPUT'); }); suite.test('Enter after last INPUT in P', function (editor) { editor.setContent('abc
'); editor.selection.setCursorLocation(editor.getBody().firstChild, 2); pressEnter(editor); LegacyUnit.equal(editor.getContent(), 'abc
\u00a0
'); }); suite.test('Enter at end of P', function (editor) { editor.setContent('abc
'); LegacyUnit.setSelection(editor, 'p', 3); pressEnter(editor); LegacyUnit.equal(editor.getContent(), 'abc
\u00a0
'); LegacyUnit.equal(editor.selection.getRng(true).startContainer.nodeName, 'P'); }); suite.test('Enter at end of EM inside P', function (editor) { editor.setContent('abc
'); LegacyUnit.setSelection(editor, 'em', 3); pressEnter(editor); LegacyUnit.equal( HtmlUtils.cleanHtml(editor.getBody().innerHTML).replace(/abc
' ); LegacyUnit.equal(editor.selection.getRng(true).startContainer.nodeName, 'EM'); }); suite.test('Enter at middle of EM inside P', function (editor) { editor.setContent('
abcd
'); LegacyUnit.setSelection(editor, 'em', 2); pressEnter(editor); LegacyUnit.equal(editor.getContent(), 'ab
cd
'); LegacyUnit.equal(editor.selection.getRng(true).startContainer.parentNode.nodeName, 'EM'); }); suite.test('Enter at beginning EM inside P', function (editor) { editor.setContent('abc
'); LegacyUnit.setSelection(editor, 'em', 0); pressEnter(editor); LegacyUnit.equal( HtmlUtils.cleanHtml(editor.getBody().innerHTML).replace(/abc
' ); LegacyUnit.equal(editor.selection.getRng(true).startContainer.nodeValue, 'abc'); }); suite.test('Enter at end of STRONG in EM inside P', function (editor) { editor.setContent('abc
'); LegacyUnit.setSelection(editor, 'strong', 3); pressEnter(editor); LegacyUnit.equal( HtmlUtils.cleanHtml(editor.getBody().innerHTML).replace(/abc
' ); LegacyUnit.equal(editor.selection.getRng(true).startContainer.nodeName, 'STRONG'); }); suite.test('Enter at middle of STRONG in EM inside P', function (editor) { editor.setContent('
abcd
'); LegacyUnit.setSelection(editor, 'strong', 2); pressEnter(editor); LegacyUnit.equal(editor.getContent(), 'ab
cd
'); LegacyUnit.equal(editor.selection.getRng(true).startContainer.parentNode.nodeName, 'STRONG'); }); suite.test('Enter at beginning STRONG in EM inside P', function (editor) { editor.setContent('abc
'); LegacyUnit.setSelection(editor, 'strong', 0); pressEnter(editor); LegacyUnit.equal( HtmlUtils.cleanHtml(editor.getBody().innerHTML).replace(/abc
' ); LegacyUnit.equal(editor.selection.getRng(true).startContainer.nodeValue, 'abc'); }); suite.test('Enter at beginning of P', function (editor) { editor.setContent('abc
'); LegacyUnit.setSelection(editor, 'p', 0); pressEnter(editor); LegacyUnit.equal(editor.getContent(), '\u00a0
abc
'); LegacyUnit.equal(editor.selection.getRng(true).startContainer.nodeValue, 'abc'); }); suite.test('Enter at middle of P with style, id and class attributes', function (editor) { editor.setContent('abcd
'); LegacyUnit.setSelection(editor, 'p', 2); pressEnter(editor); LegacyUnit.equal(editor.getContent(), 'ab
cd
'); LegacyUnit.equal(editor.selection.getRng(true).startContainer.parentNode.nodeName, 'P'); }); suite.test('Enter at a range between H1 and P', function (editor) { editor.setContent('efgh
'); LegacyUnit.setSelection(editor, 'h1', 2, 'p', 2); pressEnter(editor); LegacyUnit.equal(editor.getContent(), '| d e |
| d e |
ab
cd
'); LegacyUnit.equal(editor.selection.getNode().nodeName, 'P'); }); suite.test('Enter inside at beginning of text node in body', function (editor) { editor.getBody().innerHTML = 'abcd'; LegacyUnit.setSelection(editor, 'body', 0); pressEnter(editor); LegacyUnit.equal(editor.getContent(), '\u00a0
abcd
'); LegacyUnit.equal(editor.selection.getNode().nodeName, 'P'); }); suite.test('Enter inside at end of text node in body', function (editor) { editor.getBody().innerHTML = 'abcd'; LegacyUnit.setSelection(editor, 'body', 4); pressEnter(editor); LegacyUnit.equal(editor.getContent(), 'abcd
\u00a0
'); LegacyUnit.equal(editor.selection.getNode().nodeName, 'P'); }); suite.test('Enter inside empty body', function (editor) { editor.getBody().innerHTML = ''; LegacyUnit.setSelection(editor, 'body', 0); pressEnter(editor); LegacyUnit.equal(editor.getContent(), '\u00a0
\u00a0
'); LegacyUnit.equal(editor.selection.getNode().nodeName, 'P'); }); suite.test('Enter in the middle of text in P with forced_root_block set to false', function (editor) { editor.settings.forced_root_block = false; editor.getBody().innerHTML = 'abc
'; LegacyUnit.setSelection(editor, 'p', 2); pressEnter(editor); LegacyUnit.equal(editor.getContent(), 'ab
c
abc
'; LegacyUnit.setSelection(editor, 'p', 3); pressEnter(editor); LegacyUnit.equal(HtmlUtils.cleanHtml(editor.getBody().innerHTML), (Env.ie && Env.ie < 11) ? 'abc
abc
' : 'abc
'; LegacyUnit.setSelection(editor, 'p:last', 0); pressEnter(editor); LegacyUnit.equal(editor.getContent(), 'abc
abc
\u00a0
'); editor.settings.forced_root_block = 'p'; }); suite.test('Enter in empty P at the beginning of a blockquote and end_container_on_empty_block: true', function (editor) { editor.settings.end_container_on_empty_block = true; editor.getBody().innerHTML = (Env.ie && Env.ie < 11) ? '' : 'abc
'; LegacyUnit.setSelection(editor, 'p', 0); pressEnter(editor); LegacyUnit.equal(editor.getContent(), '
abc
\u00a0
'); editor.settings.forced_root_block = 'p'; }); suite.test('Enter in empty P at in the middle of a blockquote and end_container_on_empty_block: true', function (editor) { editor.settings.end_container_on_empty_block = true; editor.getBody().innerHTML = (Env.ie && Env.ie < 11) ? 'abc
' : 'abc
123
'; LegacyUnit.setSelection(editor, 'p:nth-child(2)', 0); pressEnter(editor); LegacyUnit.equal(editor.getContent(), 'abc
123
abc
\u00a0
'); editor.settings.forced_root_block = 'p'; }); suite.test('Enter in empty P at in the middle of a blockquote and end_container_on_empty_block: true', function (editor) { editor.settings.end_container_on_empty_block = true; editor.getBody().innerHTML = '123
'; LegacyUnit.setSelection(editor, 'p:nth-child(3)', 0); pressEnter(editor); LegacyUnit.equal(editor.getContent(), 'abc
\u00a0
123
abc
\u00a0
\u00a0
'); editor.settings.forced_root_block = 'p'; }); suite.test('Enter inside empty P with empty P siblings', function (editor) { // Tests that a workaround for an IE bug is working correctly editor.getBody().innerHTML = '123
X
'; LegacyUnit.setSelection(editor, 'p', 0); pressEnter(editor); LegacyUnit.equal(editor.getContent(), '\u00a0
\u00a0
\u00a0
X
'); }); suite.test('Enter at end of H1 with forced_root_block_attrs', function (editor) { editor.settings.forced_root_block_attrs = { class: 'class1' }; editor.getBody().innerHTML = '\u00a0
'); delete editor.settings.forced_root_block_attrs; }); suite.test('Shift+Enter at beginning of P', function (editor) { editor.getBody().innerHTML = 'abc
'; LegacyUnit.setSelection(editor, 'p', 0); pressEnter(editor, { shiftKey: true }); LegacyUnit.equal(editor.getContent(), '
abc
abcd
'; LegacyUnit.setSelection(editor, 'p', 2); pressEnter(editor, { shiftKey: true }); LegacyUnit.equal(editor.getContent(), 'ab
cd
abcd
'; LegacyUnit.setSelection(editor, 'p', 4); pressEnter(editor, { shiftKey: true }); LegacyUnit.equal(editor.getContent(), (Env.ie && Env.ie < 11) ? 'abcd
' : 'abcd
abcd
ab
cd
abcd
abcd
abc'; LegacyUnit.setSelection(editor, 'pre', 0); pressEnter(editor); LegacyUnit.equal(editor.getContent(), '
'); }); suite.test('Enter in the middle of PRE', function (editor) { editor.getBody().innerHTML = '
abc
abcd'; LegacyUnit.setSelection(editor, 'pre', 2); pressEnter(editor); LegacyUnit.equal(editor.getContent(), '
ab'); }); suite.test('Enter at the end of PRE', function (editor) { editor.getBody().innerHTML = '
cd
abcd'; LegacyUnit.setSelection(editor, 'pre', 4); pressEnter(editor); LegacyUnit.equal(editor.getContent(), (Env.ie && Env.ie < 11) ? '
abcd' : '
abcd'); }); suite.test('Enter in beginning of PRE and br_in_pre: false', function (editor) { editor.settings.br_in_pre = false; editor.getBody().innerHTML = '
abc'; LegacyUnit.setSelection(editor, 'pre', 0); pressEnter(editor); LegacyUnit.equal(editor.getContent(), '
\u00a0
abc'); delete editor.settings.br_in_pre; }); suite.test('Enter in the middle of PRE and br_in_pre: false', function (editor) { editor.settings.br_in_pre = false; editor.getBody().innerHTML = '
abcd'; LegacyUnit.setSelection(editor, 'pre', 2); pressEnter(editor); LegacyUnit.equal(editor.getContent(), '
ab
cd'); delete editor.settings.br_in_pre; }); suite.test('Enter at the end of PRE and br_in_pre: false', function (editor) { editor.settings.br_in_pre = false; editor.getBody().innerHTML = '
abcd'; LegacyUnit.setSelection(editor, 'pre', 4); pressEnter(editor); LegacyUnit.equal(editor.getContent(), '
abcd
\u00a0
'); delete editor.settings.br_in_pre; }); suite.test('Shift+Enter in beginning of PRE', function (editor) { editor.getBody().innerHTML = 'abc'; LegacyUnit.setSelection(editor, 'pre', 0); pressEnter(editor, { shiftKey: true }); LegacyUnit.equal(editor.getContent(), '
\u00a0
abc'); }); suite.test('Shift+Enter in the middle of PRE', function (editor) { editor.getBody().innerHTML = '
abcd'; LegacyUnit.setSelection(editor, 'pre', 2); pressEnter(editor, { shiftKey: true }); LegacyUnit.equal(editor.getContent(), '
ab
cd'); }); suite.test('Shift+Enter at the end of PRE', function (editor) { editor.getBody().innerHTML = '
abcd'; LegacyUnit.setSelection(editor, 'pre', 4); pressEnter(editor, { shiftKey: true }); LegacyUnit.equal(editor.getContent(), '
abcd
\u00a0
'); }); suite.test('Shift+Enter in beginning of P with forced_root_block set to false', function (editor) { editor.settings.forced_root_block = false; editor.getBody().innerHTML = 'abc
'; LegacyUnit.setSelection(editor, 'p', 0); pressEnter(editor, { shiftKey: true }); LegacyUnit.equal(editor.getContent(), '\u00a0
abc
'); editor.settings.forced_root_block = 'p'; }); suite.test('Shift+Enter in middle of P with forced_root_block set to false', function (editor) { editor.settings.forced_root_block = false; editor.getBody().innerHTML = 'abcd
'; LegacyUnit.setSelection(editor, 'p', 2); pressEnter(editor, { shiftKey: true }); LegacyUnit.equal(editor.getContent(), 'ab
cd
'); editor.settings.forced_root_block = 'p'; }); suite.test('Shift+Enter at the end of P with forced_root_block set to false', function (editor) { editor.settings.forced_root_block = false; editor.getBody().innerHTML = 'abc
'; LegacyUnit.setSelection(editor, 'p', 3); pressEnter(editor, { shiftKey: true }); LegacyUnit.equal(editor.getContent(), 'abc
\u00a0
'); editor.settings.forced_root_block = 'p'; }); suite.test('Shift+Enter in body with forced_root_block set to false', function (editor) { editor.settings.forced_root_block = false; editor.getBody().innerHTML = 'abcd'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody().firstChild, 2); rng.setEnd(editor.getBody().firstChild, 2); editor.selection.setRng(rng); pressEnter(editor, { shiftKey: true }); LegacyUnit.equal(editor.getContent(), 'ab
cd
'); editor.settings.forced_root_block = 'p'; }); suite.test('Enter at the end of DIV layer', function (editor) { editor.settings.br_in_pre = false; editor.setContent('abcd
\u00a0
X
'; LegacyUnit.setSelection(editor, 'span', 1); pressEnter(editor); LegacyUnit.equal(editor.getContent(), 'X
\u00a0
'); delete editor.settings.keep_styles; }); suite.test( 'keep_styles=false: P should not pass its styles and classes to the new P that is cloned from it when enter is pressed', function (editor) { editor.settings.keep_styles = false; editor.getBody().innerHTML = 'X
'; LegacyUnit.setSelection(editor, 'span', 1); pressEnter(editor); LegacyUnit.equal(editor.getContent(), 'X
\u00a0
'); delete editor.settings.keep_styles; }); suite.test('Enter when forced_root_block: false and force_p_newlines: true', function (editor) { editor.settings.forced_root_block = false; editor.settings.force_p_newlines = true; editor.getBody().innerHTML = 'text'; LegacyUnit.setSelection(editor, 'body', 2); pressEnter(editor); LegacyUnit.equal(editor.getContent(), 'te
xt
'); editor.settings.forced_root_block = 'p'; delete editor.settings.force_p_newlines; }); suite.test('Enter at end of br line', function (editor) { editor.settings.forced_root_block = false; editor.settings.force_p_newlines = true; editor.getBody().innerHTML = 'a
b
a
b
\u00a0
\u00a0
| x |
| x |
\u00a0
'); }); suite.test('Enter before table element', function (editor) { const rng = editor.dom.createRng(); editor.getBody().innerHTML = '| x |
\u00a0
| x |
| x |
x
'; rng.setStartAfter(editor.getBody().firstChild); rng.setEndAfter(editor.getBody().firstChild); editor.selection.setRng(rng); pressEnter(editor); LegacyUnit.equal(editor.getContent(), '| x |
\u00a0
x
'); }); suite.test('Enter before table element preceded by a p', function (editor) { const rng = editor.dom.createRng(); editor.getBody().innerHTML = 'x
| x |
x
\u00a0
| x |
| x |
\u00a0
\u00a0
| x |
abc
'); LegacyUnit.setSelection(editor, 'b', 3); pressEnter(editor); LegacyUnit.equal(editor.getContent(), 'abc
\u00a0
'); const rng = editor.selection.getRng(true); LegacyUnit.equal(rng.startContainer.nodeName, 'B'); LegacyUnit.equal(rng.startContainer.data !== ' ', true); }); suite.test('Enter inside first li with block inside', function (editor) { editor.getBody().innerHTML = 'b
>\u00a0
b
\u00a0
\u00a0
'); }); TinyLoader.setup(function (editor, onSuccess, onFailure) { Pipeline.async({}, suite.toSteps(editor), onSuccess, onFailure); }, { add_unload_trigger: false, disable_nodechange: true, schema: 'html5', extended_valid_elements: 'div[id|style|contenteditable],span[id|style|contenteditable],#dt,#dd', entities: 'raw', indent: false, skin_url: '/project/js/tinymce/skins/lightgray' }, success, failure); });