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 KeyUtils from '../module/test/KeyUtils'; import Theme from 'tinymce/themes/modern/Theme'; import { UnitTest } from '@ephox/bedrock'; UnitTest.asynctest('browser.tinymce.core.FormatterRemoveTest', function () { const success = arguments[arguments.length - 2]; const failure = arguments[arguments.length - 1]; const suite = LegacyUnit.createSuite(); Theme(); const getContent = function (editor) { return editor.getContent(editor).toLowerCase().replace(/[\r]+/g, ''); }; suite.test('Inline element on selected text', function (editor) { editor.focus(); editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

1234

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('b')[0].firstChild, 0); rng.setEnd(editor.dom.select('b')[0].firstChild, 4); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal(getContent(editor), '

1234

', 'Inline element on selected text'); }); suite.test('Inline element on selected text with remove=all', function (editor) { editor.formatter.register('format', { selector: 'b', remove: 'all' }); editor.getBody().innerHTML = '

1234

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('b')[0].firstChild, 0); rng.setEnd(editor.dom.select('b')[0].firstChild, 4); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal(getContent(editor), '

1234

', 'Inline element on selected text with remove=all'); }); suite.test('Inline element on selected text with remove=none', function (editor) { editor.formatter.register('format', { selector: 'span', styles: { fontWeight: 'bold' }, remove: 'none' }); editor.getBody().innerHTML = '

1234

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('p')[0], 0); rng.setEnd(editor.dom.select('p')[0], 1); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal(getContent(editor), '

1234

', 'Inline element on selected text with remove=none'); }); suite.test('Inline element style where element is format root', function (editor) { editor.formatter.register('format', { inline: 'span', styles: { fontWeight: 'bold' } }); editor.getBody().innerHTML = '

1234

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('em')[0].firstChild, 1); rng.setEnd(editor.dom.select('em')[0].firstChild, 3); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal(getContent(editor), '

' + '123' + '4' + '

', 'Inline element style where element is format root'); }); suite.test('Partially selected inline element text', function (editor) { editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

1234

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('b')[0].firstChild, 2); rng.setEnd(editor.dom.select('b')[0].firstChild, 4); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal(getContent(editor), '

1234

', 'Partially selected inline element text'); }); suite.test('Partially selected inline element text with children', function (editor) { editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

1234

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('span')[0].firstChild, 2); rng.setEnd(editor.dom.select('span')[0].firstChild, 4); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal( getContent(editor), '

1234

', 'Partially selected inline element text with children' ); }); suite.test('Partially selected inline element text with complex children', function (editor) { editor.formatter.register('format', { inline: 'span', styles: { fontWeight: 'bold' } }); editor.getBody().innerHTML = '

1234

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('span')[1].firstChild, 2); rng.setEnd(editor.dom.select('span')[1].firstChild, 4); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal( getContent(editor), '

12' + '34

', 'Partially selected inline element text with complex children' ); }); suite.test('Inline elements with exact flag', function (editor) { editor.formatter.register('format', { inline: 'span', styles: { color: '#ff0000' }, exact: true }); editor.getBody().innerHTML = '

12341234

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('p')[0], 0); rng.setEnd(editor.dom.select('p')[0], 2); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal( getContent(editor), '

12341234

', 'Inline elements with exact flag' ); }); suite.test('Inline elements with variables', function (editor) { editor.formatter.register('format', { inline: 'span', styles: { color: '%color' }, exact: true }); editor.getBody().innerHTML = '

12341234

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('p')[0], 0); rng.setEnd(editor.dom.select('p')[0], 2); editor.selection.setRng(rng); editor.formatter.remove('format', { color: '#ff0000' }); LegacyUnit.equal( getContent(editor), '

12341234

', 'Inline elements on selected text with variables' ); }); suite.test('Inline elements with functions and variables', function (editor) { editor.formatter.register('format', { inline: 'span', styles: { color (vars) { return vars.color + '00'; } }, exact: true }); editor.getBody().innerHTML = '

12341234

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('p')[0], 0); rng.setEnd(editor.dom.select('p')[0], 2); editor.selection.setRng(rng); editor.formatter.remove('format', { color: '#ff00' }); LegacyUnit.equal( getContent(editor), '

12341234

', 'Inline elements with functions and variables' ); }); suite.test('End within start element', function (editor) { editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

12345678

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('p')[0], 0); rng.setEnd(editor.dom.select('b')[0], 2); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal(getContent(editor), '

12345678

', 'End within start element'); }); suite.test('Start and end within similar format 1', function (editor) { editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

12345678

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('em')[0], 0); rng.setEnd(editor.dom.select('b')[1], 2); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal(getContent(editor), '

12345678

', 'Start and end within similar format 1'); }); suite.test('Start and end within similar format 2', function (editor) { editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

12345678

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('em')[0], 0); rng.setEnd(editor.dom.select('em')[0], 1); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal(getContent(editor), '

12345678

', 'Start and end within similar format 2'); }); suite.test('Start and end within similar format 3', function (editor) { editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

1234

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('em')[0], 0); rng.setEnd(editor.dom.select('em')[0], 1); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal(getContent(editor), '

1234

', 'Start and end within similar format 3'); }); suite.test('End within start', function (editor) { editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

xabcy

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('p')[0], 0); rng.setEnd(editor.dom.select('b')[1].firstChild, 3); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal(getContent(editor), '

xabcy

', 'End within start'); }); suite.test('Remove block format', function (editor) { editor.formatter.register('format', { block: 'h1' }); editor.getBody().innerHTML = '

text

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('h1')[0].firstChild, 0); rng.setEnd(editor.dom.select('h1')[0].firstChild, 4); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal(getContent(editor), '

text

', 'Remove block format'); }); suite.test('Remove wrapper block format', function (editor) { editor.formatter.register('format', { block: 'blockquote', wrapper: true }); editor.getBody().innerHTML = '

text

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('p')[0].firstChild, 0); rng.setEnd(editor.dom.select('p')[0].firstChild, 4); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal(getContent(editor), '

text

', 'Remove wrapper block format'); }); suite.test('Remove span format within block with style', function (editor) { editor.formatter.register('format', { selector: 'span', attributes: ['style', 'class'], remove: 'empty', split: true, expand: false, deep: true }); const rng = editor.dom.createRng(); editor.getBody().innerHTML = '

text

'; rng.setStart(editor.dom.select('span')[0].firstChild, 1); rng.setEnd(editor.dom.select('span')[0].firstChild, 3); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal( getContent(editor), '

text

', 'Remove span format within block with style' ); }); suite.test('Remove and verify start element', function (editor) { editor.formatter.register('format', { inline: 'b' }); const rng = editor.dom.createRng(); editor.getBody().innerHTML = '

text

'; rng.setStart(editor.dom.select('b')[0].firstChild, 1); rng.setEnd(editor.dom.select('b')[0].firstChild, 3); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal(getContent(editor), '

text

'); LegacyUnit.equal(editor.selection.getStart().nodeName, 'P'); }); suite.test('Remove with selection collapsed ensure correct caret position', function (editor) { const content = '

test

testing

'; editor.formatter.register('format', { block: 'p' }); const rng = editor.dom.createRng(); editor.getBody().innerHTML = content; rng.setStart(editor.dom.select('p')[0].firstChild, 4); rng.setEnd(editor.dom.select('p')[0].firstChild, 4); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal(getContent(editor), content); LegacyUnit.equalDom(editor.selection.getStart(), editor.dom.select('p')[0]); }); suite.test('Caret format at middle of text', function (editor) { editor.setContent('

abc

'); editor.formatter.register('format', { inline: 'b' }); LegacyUnit.setSelection(editor, 'b', 1, 'b', 1); editor.formatter.remove('format'); LegacyUnit.equal(editor.getContent(editor), '

abc

'); }); suite.test('Caret format at end of text', function (editor) { editor.setContent('

abc

'); editor.formatter.register('format', { inline: 'b' }); LegacyUnit.setSelection(editor, 'b', 3, 'b', 3); editor.formatter.remove('format'); KeyUtils.type(editor, 'd'); LegacyUnit.equal(editor.getContent(editor), '

abcd

'); }); suite.test('Caret format at end of text inside other format', function (editor) { editor.setContent('

abc

'); editor.formatter.register('format', { inline: 'b' }); LegacyUnit.setSelection(editor, 'b', 3, 'b', 3); editor.formatter.remove('format'); KeyUtils.type(editor, 'd'); LegacyUnit.equal(editor.getContent(editor), '

abcd

'); }); suite.test('Caret format at end of text inside other format with text after 1', function (editor) { editor.setContent('

abce

'); editor.formatter.register('format', { inline: 'b' }); LegacyUnit.setSelection(editor, 'b', 3, 'b', 3); editor.formatter.remove('format'); KeyUtils.type(editor, 'd'); LegacyUnit.equal(editor.getContent(editor), '

abcde

'); }); suite.test('Caret format at end of text inside other format with text after 2', function (editor) { editor.setContent('

abce

'); editor.formatter.register('format', { inline: 'em' }); LegacyUnit.setSelection(editor, 'b', 3, 'b', 3); editor.formatter.remove('format'); KeyUtils.type(editor, 'd'); LegacyUnit.equal(editor.getContent(editor), '

abcde

'); }); suite.test('Toggle styles at the end of the content don\' removes the format where it is not needed.', function (editor) { editor.setContent('

abce

'); editor.formatter.register('b', { inline: 'b' }); editor.formatter.register('em', { inline: 'em' }); LegacyUnit.setSelection(editor, 'b', 4, 'b', 4); editor.formatter.remove('b'); editor.formatter.remove('em'); LegacyUnit.equal(editor.getContent(editor), '

abce

'); }); suite.test('Caret format on second word in table cell', function (editor) { editor.setContent('
one two
'); editor.formatter.register('format', { inline: 'b' }); LegacyUnit.setSelection(editor, 'b', 2, 'b', 2); editor.formatter.remove('format'); LegacyUnit.equal(editor.getContent(editor), '
one two
'); }); suite.test('contentEditable: false on start and contentEditable: true on end', function (editor) { if (Env.ie) { // Skipped since IE doesn't support selection of parts of a cE=false element return; } editor.formatter.register('format', { inline: 'b' }); editor.setContent('

abc

def

ghj

'); const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('b')[0].firstChild, 0); rng.setEnd(editor.dom.select('b')[1].firstChild, 3); editor.selection.setRng(rng); editor.formatter.remove('format'); LegacyUnit.equal(editor.getContent(editor), '

abc

def

ghj

', 'Text in last paragraph is not bold'); }); suite.test('contentEditable: true on start and contentEditable: false on end', function (editor) { editor.formatter.register('format', { inline: 'b' }); editor.setContent('

abc

def

ghj

'); LegacyUnit.setSelection(editor, 'p:nth-child(2) b', 0, 'p:last b', 3); editor.formatter.remove('format'); LegacyUnit.equal(editor.getContent(editor), '

abc

def

ghj

', 'Text in first paragraph is not bold'); }); suite.test('contentEditable: true inside contentEditable: false', function (editor) { editor.formatter.register('format', { inline: 'b' }); editor.setContent('

abc

def

'); LegacyUnit.setSelection(editor, 'b', 0, 'b', 3); editor.formatter.remove('format'); LegacyUnit.equal(editor.getContent(editor), '

abc

def

', 'Text is not bold'); }); suite.test('remove format block on contentEditable: false block', function (editor) { editor.formatter.register('format', { block: 'h1' }); editor.setContent('

abc

def

'); LegacyUnit.setSelection(editor, 'h1:nth-child(2)', 0, 'h1:nth-child(2)', 3); editor.formatter.remove('format'); LegacyUnit.equal(editor.getContent(editor), '

abc

def

', 'H1 is still not h1'); }); suite.test('remove format on del using removeformat format', function (editor) { editor.getBody().innerHTML = '

abc

'; LegacyUnit.setSelection(editor, 'del', 0, 'del', 3); editor.formatter.remove('removeformat'); LegacyUnit.equal(HtmlUtils.cleanHtml(editor.getBody().innerHTML), '

abc

'); }); suite.test('remove format on span with class using removeformat format', function (editor) { editor.getBody().innerHTML = '

abc

'; LegacyUnit.setSelection(editor, 'span', 0, 'span', 3); editor.formatter.remove('removeformat'); LegacyUnit.equal(HtmlUtils.cleanHtml(editor.getBody().innerHTML), '

abc

'); }); suite.test('remove format on span with internal class using removeformat format', function (editor) { editor.getBody().innerHTML = '

abc

'; LegacyUnit.setSelection(editor, 'span', 0, 'span', 3); editor.formatter.remove('removeformat'); LegacyUnit.equal(HtmlUtils.normalizeHtml(HtmlUtils.cleanHtml(editor.getBody().innerHTML)), '

abc

'); }); suite.test('Remove format bug 1', function (editor) { editor.setContent('

abc

'); editor.formatter.register('format', { inline: 'b' }); LegacyUnit.setSelection(editor, 'i', 1, 'i', 2); editor.formatter.remove('format'); LegacyUnit.equal(editor.getContent(editor), '

abc

'); }); suite.test('Remove format bug 2', function (editor) { editor.setContent('

abc

'); editor.formatter.register('format', { inline: 'b' }); LegacyUnit.setSelection(editor, 'b', 0, 'b', 1); editor.formatter.remove('format'); LegacyUnit.equal(editor.getContent(editor), '

abc

'); }); suite.test('Remove format bug 3', function (editor) { editor.setContent('

ab

'); editor.formatter.register('format', { inline: 'b' }); LegacyUnit.setSelection(editor, 'i', 1, 'i', 2); editor.formatter.remove('format'); LegacyUnit.equal(editor.getContent(editor), '

ab

'); }); suite.test('Remove format with classes', function (editor) { editor.formatter.register('format', { inline: 'span', classes: ['a', 'b'] }); editor.getBody().innerHTML = '

a

'; LegacyUnit.setSelection(editor, 'span', 0, 'span', 1); editor.formatter.remove('format'); LegacyUnit.equal(getContent(editor), '

a

', 'Element should only have c left'); }); suite.test('Remove format on specified node', function (editor) { editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

a

'; editor.formatter.remove('format', {}, editor.dom.select('b')[0]); LegacyUnit.equal(getContent(editor), '

a

', 'B should be removed'); }); suite.test('Remove ceFalseOverride format', function (editor) { editor.setContent('

a

b
'); editor.formatter.register('format', [ { selector: 'div', classes: ['a'], ceFalseOverride: true }, { selector: 'p', classes: ['a'], ceFalseOverride: true } ]); editor.selection.select(editor.dom.select('div')[0]); editor.formatter.remove('format'); LegacyUnit.equal( getContent(editor), '

a

b
' ); editor.selection.select(editor.dom.select('p')[0]); editor.formatter.remove('format'); LegacyUnit.equal( getContent(editor), '

a

b
' ); }); suite.test('Remove block format from first block with forced_root_block: false', function (editor) { editor.settings.forced_root_block = false; editor.formatter.register('format', { block: 'h1' }); editor.getBody().innerHTML = '

a

b'; LegacyUnit.setSelection(editor, 'h1', 0, 'h1', 1); editor.formatter.remove('format'); LegacyUnit.equal(getContent(editor), 'a
b', 'Lines should be separated with br'); editor.settings.forced_root_block = 'p'; }); suite.test('Remove format from first position in table cell', function (editor) { editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '
ab cd
'; LegacyUnit.setSelection(editor, 'b', 0, 'b', 2); editor.formatter.remove('format'); LegacyUnit.equal(getContent(editor), '
ab cd
', 'Should have removed format.'); editor.settings.forced_root_block = 'p'; }); TinyLoader.setup(function (editor, onSuccess, onFailure) { Pipeline.async({}, suite.toSteps(editor), onSuccess, onFailure); }, { indent: false, extended_valid_elements: 'b,i,span[style|contenteditable|class]', entities: 'raw', 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,text-align' }, skin_url: '/project/js/tinymce/skins/lightgray' }, success, failure); });