import { Pipeline } from '@ephox/agar';
import { UnitTest } from '@ephox/bedrock';
import { LegacyUnit, TinyLoader } from '@ephox/mcagar';
import Env from 'tinymce/core/api/Env';
import Plugin from 'tinymce/plugins/lists/Plugin';
import Theme from 'tinymce/themes/modern/Theme';
UnitTest.asynctest('tinymce.lists.browser.ApplyTest', function () {
const success = arguments[arguments.length - 2];
const failure = arguments[arguments.length - 1];
const suite = LegacyUnit.createSuite();
Plugin();
Theme();
suite.test('Apply UL list to single P', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'
a
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'p', 0);
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getContent(), '');
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Apply UL list to single empty P', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'p', 0);
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(LegacyUnit.trimBrs(editor.getContent({ format: 'raw' })), '');
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Apply UL list to multiple Ps', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'a
' +
'b
' +
'c
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'p', 0, 'p:last', 0);
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
});
suite.test('Apply OL list to single P', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'a
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'p', 0);
LegacyUnit.execCommand(editor, 'InsertOrderedList');
LegacyUnit.equal(editor.getContent(), '- a
');
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Apply OL list to single empty P', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'p', 0);
LegacyUnit.execCommand(editor, 'InsertOrderedList');
LegacyUnit.equal(LegacyUnit.trimBrs(editor.getContent({ format: 'raw' })), '
');
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Apply OL list to multiple Ps', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'a
' +
'b
' +
'c
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'p', 0, 'p:last', 0);
LegacyUnit.execCommand(editor, 'InsertOrderedList');
LegacyUnit.equal(editor.getContent(),
'' +
'- a
' +
'- b
' +
'- c
' +
'
'
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
});
suite.test('Apply OL to UL list', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li', 0, 'li:last', 0);
LegacyUnit.execCommand(editor, 'InsertOrderedList');
LegacyUnit.equal(editor.getContent(),
'' +
'- a
' +
'- b
' +
'- c
' +
'
'
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
});
suite.test(
'Apply OL to UL list with collapsed selection',
function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'li:nth-child(2)', 0);
LegacyUnit.execCommand(editor, 'InsertOrderedList');
LegacyUnit.equal(editor.getContent(),
'' +
'- a
' +
'- b
' +
'- c
' +
'
'
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
}
);
suite.test('Apply UL to OL list', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'' +
'- a
' +
'- b
' +
'- c
' +
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'li', 0, 'li:last', 0);
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
});
suite.test('Apply UL to OL list collapsed selection', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'' +
'- a
' +
'- b
' +
'- c
' +
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'li:nth-child(2)', 0);
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
});
suite.test('Apply UL to P and merge with adjacent lists', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'' +
'b
' +
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'p', 1);
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
});
suite.test('Apply UL to OL and merge with adjacent lists', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'' +
'- b
' +
''
);
editor.focus();
LegacyUnit.setSelection(editor, 'ol li', 1);
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
});
suite.test('Apply OL to P and merge with adjacent lists', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'' +
'- a
' +
'
' +
'b
' +
'' +
'- c
' +
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'p', 1);
LegacyUnit.execCommand(editor, 'InsertOrderedList');
LegacyUnit.equal(editor.getContent(),
'' +
'- a
' +
'- b
' +
'- c
' +
'
'
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
});
suite.test('Apply OL to UL and merge with adjacent lists', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'' +
'- 1a
' +
'- 1b
' +
'
' +
'' +
'' +
'- 3a
' +
'- 3b
' +
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'ul li', 1);
LegacyUnit.execCommand(editor, 'InsertOrderedList');
LegacyUnit.equal(editor.getContent(),
'' +
'- 1a
' +
'- 1b
' +
'- 2a
' +
'- 2b
' +
'- 3a
' +
'- 3b
' +
'
'
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
});
suite.test(
'Apply OL to UL and DO not merge with adjacent lists because styles are different (exec has style)',
function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'' +
'- a
' +
'
' +
'' +
'' +
'- c
' +
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'ul li', 1);
LegacyUnit.execCommand(editor, 'InsertOrderedList', null, { 'list-style-type': 'lower-alpha' });
LegacyUnit.equal(editor.getContent(),
'' +
'- a
' +
'
' +
'- b
' +
'' +
'- c
' +
'
'
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
}
);
suite.test(
'Apply OL to P and DO not merge with adjacent lists because styles are different (exec has style)',
function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'' +
'- a
' +
'
' +
'b
' +
'' +
'- c
' +
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'p', 1);
LegacyUnit.execCommand(editor, 'InsertOrderedList', null, { 'list-style-type': 'lower-alpha' });
LegacyUnit.equal(editor.getContent(),
'' +
'- a
' +
'
' +
'- b
' +
'' +
'- c
' +
'
'
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
}
);
suite.test(
'Apply OL to UL and DO not merge with adjacent lists because styles are different (original has style)',
function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'' +
'- a
' +
'
' +
'' +
'' +
'- c
' +
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'ul li', 1);
LegacyUnit.execCommand(editor, 'InsertOrderedList');
LegacyUnit.equal(editor.getContent(),
'' +
'- a
' +
'
' +
'- b
' +
'' +
'- c
' +
'
'
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
}
);
suite.test(
'Apply OL to UL should merge with adjacent lists because styles are the same (both have roman)',
function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'' +
'- a
' +
'
' +
'' +
'' +
'- c
' +
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'ul li', 1);
LegacyUnit.execCommand(editor, 'InsertOrderedList', false, { 'list-style-type': 'upper-roman' });
LegacyUnit.equal(editor.getContent(),
'' +
'- a
' +
'- b
' +
'- c
' +
'
'
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
}
);
suite.test(
'Apply OL to UL should merge with above list because styles are the same (both have lower-roman), but not below list',
function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'' +
'- a
' +
'
' +
'' +
'' +
'- c
' +
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'ul li', 1);
LegacyUnit.execCommand(editor, 'InsertOrderedList', false, { 'list-style-type': 'lower-roman' });
LegacyUnit.equal(editor.getContent(),
'' +
'- a
' +
'- b
' +
'
' +
'' +
'- c
' +
'
'
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
}
);
suite.test(
'Apply OL to UL should merge with below lists because styles are the same (both have roman), but not above list',
function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'' +
'- a
' +
'
' +
'' +
'' +
'- c
' +
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'ul li', 1);
LegacyUnit.execCommand(editor, 'InsertOrderedList', false, { 'list-style-type': 'lower-roman' });
LegacyUnit.equal(editor.getContent(),
'' +
'- a
' +
'
' +
'' +
'- b
' +
'- c
' +
'
'
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
}
);
suite.test(
'Apply OL to UL and DO not merge with adjacent lists because classes are different',
function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'' +
'- a
' +
'
' +
'' +
'' +
'- c
' +
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'ul li', 1);
LegacyUnit.execCommand(editor, 'InsertOrderedList');
LegacyUnit.equal(editor.getContent(),
'' +
'- a
' +
'
' +
'- b
' +
'' +
'- c
' +
'
'
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
}
);
suite.test('Apply UL list to single text line', function (editor) {
editor.settings.forced_root_block = false;
editor.getBody().innerHTML = (
'a'
);
editor.focus();
LegacyUnit.setSelection(editor, 'body', 0);
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getContent(), '');
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
editor.settings.forced_root_block = 'p';
});
suite.test('Apply UL list to single text line with BR', function (editor) {
editor.settings.forced_root_block = false;
editor.getBody().innerHTML = (
'a
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'body', 0);
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getContent(), '');
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
editor.settings.forced_root_block = 'p';
});
suite.test('Apply UL list to multiple lines separated by BR', function (editor) {
editor.settings.forced_root_block = false;
editor.getBody().innerHTML = (
'a
' +
'b
' +
'c'
);
editor.focus();
editor.execCommand('SelectAll');
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
editor.settings.forced_root_block = 'p';
});
suite.test(
'Apply UL list to multiple lines separated by BR and with trailing BR',
function (editor) {
editor.settings.forced_root_block = false;
editor.getBody().innerHTML = (
'a
' +
'b
' +
'c
'
);
editor.focus();
editor.execCommand('SelectAll');
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
}
);
suite.test('Apply UL list to multiple formatted lines separated by BR', function (editor) {
editor.settings.forced_root_block = false;
editor.getBody().innerHTML = (
'a
' +
'b
' +
'c'
);
editor.focus();
LegacyUnit.setSelection(editor, 'strong', 0, 'em', 0);
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'STRONG');
// Old IE will return the end LI not a big deal
LegacyUnit.equal(editor.selection.getEnd().nodeName, Env.ie && Env.ie < 9 ? 'LI' : 'EM');
});
// Ignore on IE 7, 8 this is a known bug not worth fixing
if (!Env.ie || Env.ie > 8) {
suite.test('Apply UL list to br line and text block line', function (editor) {
editor.settings.forced_root_block = false;
editor.setContent(
'a' +
'b
'
);
const rng = editor.dom.createRng();
rng.setStart(editor.getBody().firstChild, 0);
rng.setEnd(editor.getBody().lastChild.firstChild, 1);
editor.selection.setRng(rng);
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
LegacyUnit.equal(editor.selection.getEnd().nodeName, 'LI');
});
}
suite.test('Apply UL list to text block line and br line', function (editor) {
editor.settings.forced_root_block = false;
editor.getBody().innerHTML = (
'a
' +
'b'
);
editor.focus();
const rng = editor.dom.createRng();
rng.setStart(editor.getBody().firstChild.firstChild, 0);
rng.setEnd(editor.getBody().lastChild, 1);
editor.selection.setRng(rng);
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getContent(),
''
);
LegacyUnit.equal(editor.selection.getStart().nodeName, 'LI');
LegacyUnit.equal(editor.selection.getEnd().nodeName, 'LI');
});
suite.test('Apply UL list to all BR lines (SelectAll)', function (editor) {
editor.settings.forced_root_block = false;
editor.getBody().innerHTML = (
'a
' +
'b
' +
'c
'
);
editor.focus();
editor.execCommand('SelectAll');
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getContent(),
''
);
editor.settings.forced_root_block = 'p';
});
suite.test('Apply UL list to all P lines (SelectAll)', function (editor) {
editor.getBody().innerHTML = (
'a
' +
'b
' +
'c
'
);
editor.focus();
editor.execCommand('SelectAll');
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getContent(),
''
);
});
suite.test('Apply UL list to single P', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'a
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'p', 0);
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getContent(), '');
LegacyUnit.equal(editor.selection.getNode().nodeName, 'LI');
});
suite.test('Apply UL list to more than two paragraphs', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs(
'a
' +
'b
' +
'c
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'p:nth-child(1)', 0, 'p:nth-child(3)', 1);
LegacyUnit.execCommand(editor, 'InsertUnorderedList', false, { 'list-style-type': null });
LegacyUnit.equal(editor.getContent(), '');
});
suite.test('Apply UL with custom attributes', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs('a
');
editor.focus();
LegacyUnit.setSelection(editor, 'p', 0);
LegacyUnit.execCommand(editor, 'InsertUnorderedList', false, {
'list-attributes': {
'class': 'a',
'data-custom': 'c1'
}
});
LegacyUnit.equal(editor.getContent(), '');
});
suite.test('Apply UL and LI with custom attributes', function (editor) {
editor.getBody().innerHTML = LegacyUnit.trimBrs('a
');
editor.focus();
LegacyUnit.setSelection(editor, 'p', 0);
LegacyUnit.execCommand(editor, 'InsertUnorderedList', false, {
'list-attributes': {
'class': 'a',
'data-custom': 'c1'
},
'list-item-attributes': {
'class': 'b',
'data-custom1': 'c2',
'data-custom2': ''
}
});
LegacyUnit.equal(editor.getContent(), '');
});
suite.test('Handle one empty unordered list items without error', function (editor) {
editor.getBody().innerHTML = (
''
);
editor.execCommand('SelectAll');
LegacyUnit.setSelection(editor, 'li:first', 0, 'li:last', 0);
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getBody().innerHTML,
'a
' +
'b
' +
'
'
);
});
suite.test('Handle several empty unordered list items without error', function (editor) {
editor.getBody().innerHTML = (
'' +
'- a
' +
'- b
' +
'' +
'- c
' +
'' +
'- d
' +
'' +
'- e
' +
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'li:first', 0, 'li:last', 0);
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getBody().innerHTML,
'a
' +
'b
' +
'
' +
'c
' +
'
' +
'd
' +
'
' +
'e
'
);
});
suite.test('Handle one empty ordered list items without error', function (editor) {
editor.getBody().innerHTML = (
'' +
'- a
' +
'- b
' +
'' +
'
'
);
editor.execCommand('SelectAll');
LegacyUnit.setSelection(editor, 'li:first', 0, 'li:last', 0);
LegacyUnit.execCommand(editor, 'InsertOrderedList');
LegacyUnit.equal(editor.getBody().innerHTML,
'a
' +
'b
' +
'
'
);
});
suite.test('Handle several empty ordered list items without error', function (editor) {
editor.getBody().innerHTML = (
'' +
'- a
' +
'- b
' +
'' +
'- c
' +
'' +
'- d
' +
'' +
'- e
' +
'
'
);
editor.focus();
LegacyUnit.setSelection(editor, 'li:first', 0, 'li:last', 0);
LegacyUnit.execCommand(editor, 'InsertOrderedList');
LegacyUnit.equal(editor.getBody().innerHTML,
'a
' +
'b
' +
'
' +
'c
' +
'
' +
'd
' +
'
' +
'e
'
);
});
suite.test('Apply list on paragraphs with list between', function (editor) {
editor.getBody().innerHTML = (
'a
' +
'' +
'- b
' +
'
' +
'c
'
);
editor.execCommand('SelectAll');
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getBody().innerHTML, '- b
');
});
suite.test('Apply unordered list on children on a fully selected ordered list', function (editor) {
editor.getBody().innerHTML = (
'' +
'- a' +
'
' +
'- b
' +
'
' +
' ' +
'- c
' +
'
'
);
editor.execCommand('SelectAll');
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getBody().innerHTML, '');
});
suite.test('Apply unordered list on empty table cell', function (editor) {
editor.getBody().innerHTML = (
'' +
'' +
'' +
'' +
' ' +
' | ' +
'
' +
'' +
'
'
);
const rng = editor.dom.createRng();
rng.setStart(editor.dom.select('td')[0], 0);
rng.setEnd(editor.dom.select('td')[0], 1);
editor.selection.setRng(rng);
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getBody().innerHTML, '');
});
suite.test('Apply unordered list on table cell with two lines br', function (editor) {
editor.getBody().innerHTML = (
'' +
'' +
'' +
'' +
'a b' +
' | ' +
'
' +
'' +
'
'
);
const rng = editor.dom.createRng();
rng.setStart(editor.dom.select('td')[0].firstChild, 0);
rng.setEnd(editor.dom.select('td')[0].firstChild, 0);
editor.selection.setRng(rng);
LegacyUnit.execCommand(editor, 'InsertUnorderedList');
LegacyUnit.equal(editor.getBody().innerHTML, '');
});
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|data-custom1|data-custom2],ol[style|class|data-custom|data-custom1|data-custom2],' +
'ul[style|class|data-custom|data-custom1|data-custom2],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'
},
skin_url: '/project/js/tinymce/skins/lightgray'
}, success, failure);
});