import { Pipeline } from '@ephox/agar';
import { UnitTest } from '@ephox/bedrock';
import { LegacyUnit, TinyLoader } from '@ephox/mcagar';
import Plugin from 'tinymce/plugins/lists/Plugin';
import Theme from 'tinymce/themes/modern/Theme';
UnitTest.asynctest('tinymce.lists.browser.BackspaceDeleteTest', function () {
const success = arguments[arguments.length - 2];
const failure = arguments[arguments.length - 1];
const suite = LegacyUnit.createSuite();
Plugin();
Theme();
suite.test('Backspace at beginning of single LI in UL', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'li', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(editor.getContent(),
'a
'
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'P');
});
suite.test('Backspace at beginning of first LI in UL', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(editor.getContent(),
'a
' +
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'P');
});
suite.test('Backspace at beginning of middle LI in UL', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li:nth-child(2)', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Backspace at beginning of start LI in UL inside UL', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li li', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Backspace at beginning of middle LI in UL inside UL', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li li:nth-child(2)', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Backspace at beginning of single LI in UL', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(editor.getContent(),
'a
'
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'P');
});
suite.test('Backspace at beginning of first LI in UL', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(editor.getContent(),
'a
' +
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'P');
});
suite.test('Backspace at beginning of middle LI in UL', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li:nth-child(2)', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Backspace at beginning of start LI in UL inside UL', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li li', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Backspace at beginning of middle LI in UL inside UL', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li li:nth-child(2)', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Backspace at beginning of LI with empty LI above in UL', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li:nth-child(3)', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().innerHTML, 'b');
});
suite.test('Backspace at beginning of LI with BR padded empty LI above in UL', function (editor) {
editor.getBody().innerHTML = (
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li:nth-child(3)', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().innerHTML, 'b');
});
suite.test('Backspace at empty LI (IE)', function (editor) {
editor.getBody().innerHTML = (
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li:nth-child(2)', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().innerHTML, 'a');
});
suite.test('Backspace at beginning of LI with empty LI with STRING and BR above in UL', function (editor) {
editor.getBody().innerHTML = (
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li:nth-child(3)', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().innerHTML, 'b');
});
suite.test('Backspace at nested LI with adjacent BR', function (editor) {
editor.getBody().innerHTML = (
'' +
'- 1' +
'
' +
'- ' +
'
' +
'' +
' ' +
'
' +
' ' +
'- 3
' +
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'ul ul ul li', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(editor.getContent(), '');
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Backspace at LI selected with triple-click in UL', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'' +
'- a
' +
'- b' +
'' +
'
' +
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'li:nth-child(1)', 0, 'li:nth-child(2)', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(LegacyUnit.trimBrs(editor.getContent()),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Backspace at partially selected list', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'abc
' +
'' +
'- a
' +
'- b' +
'' +
'
' +
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'p', 1, 'li:nth-child(2)', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(LegacyUnit.trimBrs(editor.getContent()),
'ab
' +
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'P');
});
// Delete
suite.test('Delete at end of single LI in UL', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li', 1);
editor.plugins.lists.backspaceDelete(true);
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Delete at end of first LI in UL', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li', 1);
editor.plugins.lists.backspaceDelete(true);
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Delete at end of middle LI in UL', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li:nth-child(2)', 1);
editor.plugins.lists.backspaceDelete(true);
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Delete at end of start LI in UL inside UL', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li li', 1);
editor.plugins.lists.backspaceDelete(true);
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Delete at end of middle LI in UL inside UL', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li li:nth-child(2)', 1);
editor.plugins.lists.backspaceDelete(true);
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Delete at end of LI before empty LI', function (editor) {
editor.getBody().innerHTML = (
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li', 1);
editor.plugins.lists.backspaceDelete(true);
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().innerHTML, 'a');
});
suite.test('Delete at end of LI before BR padded empty LI', function (editor) {
editor.getBody().innerHTML = (
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li', 1);
editor.plugins.lists.backspaceDelete(true);
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().innerHTML, 'a');
});
suite.test('Delete at end of LI before empty LI with STRONG', function (editor) {
editor.getBody().innerHTML = (
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li', 1);
editor.plugins.lists.backspaceDelete(true);
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().innerHTML, 'a');
});
suite.test('Delete at nested LI with adjacent BR', function (editor) {
editor.getBody().innerHTML = (
'' +
'- 1' +
'
' +
'- ' +
'
' +
'' +
' ' +
'
' +
' ' +
'- 3
' +
'
'
);
editor.focus();
editor.selection.setCursorLocation(editor.$('ul ul li')[0], 0);
editor.plugins.lists.backspaceDelete(true);
LegacyUnit.equal(editor.getContent(), '');
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Delete at BR before text in LI', function (editor) {
editor.getBody().innerHTML = (
''
);
editor.focus();
editor.selection.setCursorLocation(editor.$('li')[1], 1);
editor.plugins.lists.backspaceDelete(false);
LegacyUnit.equal(editor.getContent(), '');
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Backspace merge li elements', function (editor) {
// IE allows you to place the caret inside a LI without children
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li:nth-child(2)', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
LegacyUnit.equal(editor.selection.getRng(true).startContainer.nodeType, 3, 'Should be a text node');
});
suite.test('Backspace at block inside li element into li without block element', function (editor) {
editor.getBody().innerHTML = (
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'p', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(
editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Backspace at block inside li element into li with block element', function (editor) {
editor.getBody().innerHTML = (
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li:nth-child(2) p', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(
editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'P');
});
suite.test('Backspace at block inside li element into li with multiple block elements', function (editor) {
editor.getBody().innerHTML = (
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li:nth-child(2) p', 0);
editor.plugins.lists.backspaceDelete();
LegacyUnit.equal(
editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Delete at block inside li element into li without block element', function (editor) {
editor.getBody().innerHTML = (
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'p', 1);
editor.plugins.lists.backspaceDelete(true);
LegacyUnit.equal(
editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'P');
});
suite.test('Delete at block inside li element into li with block element', function (editor) {
editor.getBody().innerHTML = (
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li:nth-child(1) p', 1);
editor.plugins.lists.backspaceDelete(true);
LegacyUnit.equal(
editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'P');
});
suite.test('Delete at block inside li element into li with multiple block elements', function (editor) {
editor.getBody().innerHTML = (
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li:nth-child(1)', 1);
editor.plugins.lists.backspaceDelete(true);
LegacyUnit.equal(
editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
TinyLoader.setup(function (editor, onSuccess, onFailure) {
Pipeline.async({}, suite.toSteps(editor), onSuccess, onFailure);
}, {
plugins: 'lists',
add_unload_trigger: false,
disable_nodechange: true,
indent: false,
entities: 'raw',
valid_elements:
'li[style|class|data-custom],ol[style|class|data-custom],' +
'ul[style|class|data-custom],dl,dt,dd,em,strong,span,#p,div,br',
valid_styles: {
'*': 'color,font-size,font-family,background-color,font-weight,' +
'font-style,text-decoration,float,margin,margin-top,margin-right,' +
'margin-bottom,margin-left,display,position,top,left,list-style-type'
},
content_style: '.mce-content-body { line-height: normal; }', // Breaks tests in phantomjs unless we have this
skin_url: '/project/js/tinymce/skins/lightgray'
}, success, failure);
});