import { Pipeline } from '@ephox/agar';
import { UnitTest } from '@ephox/bedrock';
import { LegacyUnit, TinyLoader } from '@ephox/mcagar';
import Tools from 'tinymce/core/api/util/Tools';
import Plugin from 'tinymce/plugins/toc/Plugin';
import Theme from 'tinymce/themes/modern/Theme';
import HtmlUtils from '../module/test/HtmlUtils';
UnitTest.asynctest('browser.tinymce.plugins.toc.TocPluginTest', function () {
const success = arguments[arguments.length - 2];
const failure = arguments[arguments.length - 1];
const suite = LegacyUnit.createSuite();
Plugin();
Theme();
const stripAttribs = function ($el, attr) {
if (Tools.isArray(attr)) {
Tools.each(attr, function (attr) {
stripAttribs($el, attr);
});
return;
}
$el.removeAttr(attr);
$el.find('[' + attr + ']').removeAttr(attr);
};
const trimBr = function (html) {
return html.replace(/
/g, '');
};
suite.test('mceInsertToc', function (editor) {
editor.getBody().innerHTML =
'
This is some text.
This is some text.
This is some text.
' + 'This is some text.
' ; LegacyUnit.setSelection(editor, 'h1', 0); editor.execCommand('mceInsertToc'); const $toc = editor.$('.tst-toc'); LegacyUnit.equal($toc.length, 2, 'ToC inserted'); LegacyUnit.equal($toc.attr('contentEditable'), 'false', 'cE=false'); LegacyUnit.equal($toc.find('ul ul ul').length, 0, 'no levels beyond 2 are included'); stripAttribs($toc, ['data-mce-href', 'data-mce-selected']); LegacyUnit.equal(trimBr(HtmlUtils.normalizeHtml($toc[0].outerHTML)), '', 'no surprises in ToC structure' ); }); suite.test('mceInsertToc - flat structure', function (editor) { editor.getBody().innerHTML = 'This is some text.
This is some text.
This is some text.
' + 'This is some text.
' ; LegacyUnit.setSelection(editor, 'h1', 0); editor.execCommand('mceInsertToc'); const $toc = editor.$('.tst-toc'); stripAttribs($toc, ['data-mce-href', 'data-mce-selected']); LegacyUnit.equal(trimBr(HtmlUtils.normalizeHtml($toc[0].innerHTML)), 'This is some text.
This is some text.
This is some text.
' + 'This is some text.
' ; LegacyUnit.setSelection(editor, 'h1', 0); editor.execCommand('mceInsertToc'); // add one more heading editor.$().append('This is some text.
'); LegacyUnit.setSelection(editor, 'li', 0); editor.execCommand('mceUpdateToc'); LegacyUnit.equal(editor.$('.tst-toc > ul a[href="#h5"]').length, 1, 'ToC has been successfully updated'); }); suite.test('Misc.', function (editor) { let contents, $toc; editor.getBody().innerHTML = 'This is some text.
This is some text.
' + 'This is some text.
' ; LegacyUnit.setSelection(editor, 'h2', 0); editor.execCommand('mceInsertToc'); contents = editor.getContent(); LegacyUnit.equal(/contenteditable/i.test(contents), false, 'cE stripped for getContent()'); editor.setContent(contents); $toc = editor.$('.tst-toc'); LegacyUnit.deepEqual($toc.attr('contentEditable'), 'false', 'cE added back after setContent()'); LegacyUnit.deepEqual($toc.find(':first-child').attr('contentEditable'), 'true', 'cE added back to title after setContent()'); stripAttribs($toc, ['data-mce-href', 'data-mce-selected']); LegacyUnit.equal(trimBr(HtmlUtils.normalizeHtml($toc[0].innerHTML)), '