import { describe, it } from '@ephox/bedrock-client'; import { LegacyUnit, TinyAssertions, TinyHooks, TinySelections } from '@ephox/wrap-mcagar'; import { assert } from 'chai'; import Editor from 'tinymce/core/api/Editor'; import { ZWSP } from 'tinymce/core/text/Zwsp'; import * as KeyUtils from '../module/test/KeyUtils'; describe('browser.tinymce.core.FormatterRemoveTest', () => { const hook = TinyHooks.bddSetupLight({ indent: false, extended_valid_elements: 'b[style],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' }, base_url: '/project/tinymce/js/tinymce' }, []); const getContent = (editor: Editor) => { return editor.getContent().toLowerCase().replace(/[\r]+/g, ''); }; it('Inline element on selected text', () => { const editor = hook.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 as Text, 0); rng.setEnd(editor.dom.select('b')[0].firstChild as Text, 4); editor.selection.setRng(rng); editor.formatter.remove('format'); assert.equal(getContent(editor), '

1234

', 'Inline element on selected text'); }); it('Inline element on selected text with remove=all', () => { const editor = hook.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 as Text, 0); rng.setEnd(editor.dom.select('b')[0].firstChild as Text, 4); editor.selection.setRng(rng); editor.formatter.remove('format'); assert.equal(getContent(editor), '

1234

', 'Inline element on selected text with remove=all'); }); it('Inline element on selected text with remove=none', () => { const editor = hook.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'); assert.equal(getContent(editor), '

1234

', 'Inline element on selected text with remove=none'); }); it('Inline element style where element is format root', () => { const editor = hook.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 as Text, 1); rng.setEnd(editor.dom.select('em')[0].firstChild as Text, 3); editor.selection.setRng(rng); editor.formatter.remove('format'); assert.equal(getContent(editor), '

' + '123' + '4' + '

', 'Inline element style where element is format root'); }); it('Partially selected inline element text', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

1234

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

1234

', 'Partially selected inline element text'); }); it('Partially selected inline element text with children', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

1234

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

1234

', 'Partially selected inline element text with children'); }); it('Partially selected inline element text with complex children', () => { const editor = hook.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 as Text, 2); rng.setEnd(editor.dom.select('span')[1].firstChild as Text, 4); editor.selection.setRng(rng); editor.formatter.remove('format'); assert.equal(getContent(editor), '

12' + '34

', 'Partially selected inline element text with complex children'); }); it('Inline elements with exact flag', () => { const editor = hook.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'); assert.equal(getContent(editor), '

12341234

', 'Inline elements with exact flag'); }); it('Inline elements with variables', () => { const editor = hook.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' }); assert.equal(getContent(editor), '

12341234

', 'Inline elements on selected text with variables'); }); it('Inline elements with functions and variables', () => { const editor = hook.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' }); assert.equal(getContent(editor), '

12341234

', 'Inline elements with functions and variables'); }); it('End within start element', () => { const editor = hook.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'); assert.equal(getContent(editor), '

12345678

', 'End within start element'); }); it('Start and end within similar format 1', () => { const editor = hook.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'); assert.equal(getContent(editor), '

12345678

', 'Start and end within similar format 1'); }); it('Start and end within similar format 2', () => { const editor = hook.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'); assert.equal(getContent(editor), '

12345678

', 'Start and end within similar format 2'); }); it('Start and end within similar format 3', () => { const editor = hook.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'); assert.equal(getContent(editor), '

1234

', 'Start and end within similar format 3'); }); it('End within start', () => { const editor = hook.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 as Text, 3); editor.selection.setRng(rng); editor.formatter.remove('format'); assert.equal(getContent(editor), '

xabcy

', 'End within start'); }); it('Remove block format', () => { const editor = hook.editor(); editor.formatter.register('format', { block: 'h1' }); editor.getBody().innerHTML = '

text

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

text

', 'Remove block format'); }); it('Remove wrapper block format', () => { const editor = hook.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 as Text, 0); rng.setEnd(editor.dom.select('p')[0].firstChild as Text, 4); editor.selection.setRng(rng); editor.formatter.remove('format'); assert.equal(getContent(editor), '

text

', 'Remove wrapper block format'); }); it('Remove span format within block with style', () => { const editor = hook.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 as Text, 1); rng.setEnd(editor.dom.select('span')[0].firstChild as Text, 3); editor.selection.setRng(rng); editor.formatter.remove('format'); assert.equal(getContent(editor), '

text

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

text

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

text

'); assert.equal(editor.selection.getStart().nodeName, 'P'); }); it('Remove with selection collapsed ensure correct caret position', () => { const editor = hook.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 as Text, 4); rng.setEnd(editor.dom.select('p')[0].firstChild as Text, 4); editor.selection.setRng(rng); editor.formatter.remove('format'); assert.equal(getContent(editor), content); LegacyUnit.equalDom(editor.selection.getStart(), editor.dom.select('p')[0]); }); it('Caret format at middle of text', () => { const editor = hook.editor(); editor.setContent('

abc

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

abc

'); }); it('Caret format at end of text', () => { const editor = hook.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'); TinyAssertions.assertContent(editor, '

abcd

'); }); it('Caret format at end of text inside other format', () => { const editor = hook.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'); TinyAssertions.assertContent(editor, '

abcd

'); }); it('Caret format at end of text inside other format with text after 1', () => { const editor = hook.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'); TinyAssertions.assertContent(editor, '

abcde

'); }); it('Caret format at end of text inside other format with text after 2', () => { const editor = hook.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'); TinyAssertions.assertContent(editor, '

abcde

'); }); it(`Toggle styles at the end of the content don' removes the format where it is not needed.`, () => { const editor = hook.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'); TinyAssertions.assertContent(editor, '

abce

'); }); it('Caret format on second word in table cell', () => { const editor = hook.editor(); editor.setContent('
one two
'); editor.formatter.register('format', { inline: 'b' }); LegacyUnit.setSelection(editor, 'b', 2, 'b', 2); editor.formatter.remove('format'); TinyAssertions.assertContent(editor, '
one two
'); }); it('contentEditable: false on start and contentEditable: true on end', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.setContent('

abc

def

ghj

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

abc

def

ghj

', 'Text in last paragraph is not bold'); }); it('contentEditable: true on start and contentEditable: false on end', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.setContent('

abc

def

ghj

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

abc

def

ghj

', 'Text in first paragraph is not bold'); }); it('contentEditable: true inside contentEditable: false', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.setContent('

abc

def

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

abc

def

', 'Text is not bold'); }); it('remove format block on contentEditable: false block', () => { const editor = hook.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'); assert.equal(editor.getContent(), '

abc

def

', 'H1 is still not h1'); }); it('remove format on del using removeformat format', () => { const editor = hook.editor(); editor.getBody().innerHTML = '

abc

'; LegacyUnit.setSelection(editor, 'del', 0, 'del', 3); editor.formatter.remove('removeformat'); TinyAssertions.assertContent(editor, '

abc

'); }); it('remove format on span with class using removeformat format', () => { const editor = hook.editor(); editor.getBody().innerHTML = '

abc

'; LegacyUnit.setSelection(editor, 'span', 0, 'span', 3); editor.formatter.remove('removeformat'); TinyAssertions.assertContent(editor, '

abc

'); }); it('remove format on span with internal class using removeformat format', () => { const editor = hook.editor(); editor.getBody().innerHTML = '

abc

'; LegacyUnit.setSelection(editor, 'span', 0, 'span', 3); editor.formatter.remove('removeformat'); TinyAssertions.assertRawContent(editor, '

abc

'); }); it('Remove format of nested elements at start', () => { const editor = hook.editor(); editor.setContent('

abc

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

abc

'); }); it('Remove format of nested elements at end', () => { const editor = hook.editor(); editor.setContent('

abc

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

abc

'); }); it('Remove format of nested elements at end with text after ', () => { const editor = hook.editor(); editor.setContent('

abcd

'); editor.formatter.register('format', { inline: 'b' }); LegacyUnit.setSelection(editor, 'i', 0, 'i', 2); editor.formatter.remove('format'); TinyAssertions.assertContent(editor, '

abcd

'); }); it('Remove format bug 2', () => { const editor = hook.editor(); editor.setContent('

abc

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

abc

'); }); it('Remove format bug 3', () => { const editor = hook.editor(); editor.setContent('

ab

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

ab

'); }); it('Remove format with classes', () => { const editor = hook.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'); assert.equal(getContent(editor), '

a

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

a

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

a

', 'B should be removed'); }); it('Remove ceFalseOverride format', () => { const editor = hook.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'); assert.equal(getContent(editor), '

a

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

a

b
'); }); it('Remove format from first position in table cell', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '
ab cd
'; LegacyUnit.setSelection(editor, 'b', 0, 'b', 2); editor.formatter.remove('format'); assert.equal(getContent(editor), '
ab cd
', 'Should have removed format.'); }); it('Remove format from last position in table cell', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '
ab cd
'; LegacyUnit.setSelection(editor, 'b', 0, 'tr', 2); editor.formatter.remove('format'); assert.equal(getContent(editor), '
ab cd
', 'Should have removed format.'); }); it('Inline element on selected text with preserve_attributes flag (bold)', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b', preserve_attributes: [ 'class', 'style' ], remove: 'all' }); editor.getBody().innerHTML = '

1234

'; LegacyUnit.setSelection(editor, 'b', 2, 'b', 2); editor.formatter.remove('format'); assert.equal(getContent(editor), '

1234

', 'Inline element on selected text with preserve_attributes flag (bold)'); }); it('Complex inline element using ranged selection with preserve_attributes flag (bold)', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b', preserve_attributes: [ 'class', 'style' ], remove: 'all' }); editor.getBody().innerHTML = '

If the situation of the pandemic does not improve, there will be no spectators, no museum, no stores open, and money will continue to be lost.

'; LegacyUnit.setSelection(editor, 'b', 24, 'span', 56); editor.formatter.remove('format'); assert.equal( getContent(editor), '

' + 'if the situation of the ' + 'pandemic does not improve, there will be no spectators, no museum, no stores open' + ', and money will continue to be lost.' + '

', 'Inline element on selected text with preserve_attributes flag (bold)'); }); it('Inline element on selected text with preserve_attributes flag (italic)', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'em', preserve_attributes: [ 'class', 'style' ], remove: 'all' }); editor.getBody().innerHTML = '

1234

'; LegacyUnit.setSelection(editor, 'em', 2, 'em', 2); editor.formatter.remove('format'); assert.equal(getContent(editor), '

1234

', 'Inline element on text with preserve_attributes flag (italic)'); }); it('Remove color format on text with multiple underline text decorations', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'span', exact: true, styles: { color: '#ff0000' } }); editor.setContent('

abc def ghi

'); editor.selection.select(editor.dom.select('span')[1]); editor.formatter.remove('format'); assert.equal(getContent(editor), '

abc def ghi

', 'Remove color format on text with multiple underline text decorations'); }); it('Remove format on node outside fake table selection', () => { const editor = hook.editor(); editor.setContent('

test

cell 1cell 2
cell 3cell 4
'); LegacyUnit.setSelection(editor, 'td', 0, 'td', 0); const para = editor.dom.select('p')[0]; // Remove bold on custom node editor.formatter.remove('bold', { }, para); assert.equal(getContent(editor), '

test

cell 1cell 2
cell 3cell 4
'); // Remove bold current fake table selection editor.formatter.remove('bold'); assert.equal(getContent(editor), '

test

cell 1cell 2
cell 3cell 4
'); }); it('TINY-6268: Remove inline format on text range selection with adjacent spaces', () => { const editor = hook.editor(); editor.setContent('

test test

'); LegacyUnit.setSelection(editor, 'span', 1, 'span', 2); editor.formatter.remove('underline'); assert.equal(getContent(editor), '

test test

', 'Formatting on the space should not have been removed'); }); it('TINY-6268: Remove inline format on text collapsed selection with adjacent spaces', () => { const editor = hook.editor(); editor.setContent('

test test

'); LegacyUnit.setSelection(editor, 'span', 1, 'span', 1); editor.formatter.remove('underline'); assert.equal(getContent(editor), '

test test

', 'Formatting on the space should not have been removed'); }); it('TINY-7227: Remove classes with variables', () => { const editor = hook.editor(); editor.formatter.register('formatA', { selector: 'p', classes: [ '%value' ] }); editor.setContent('

test

'); LegacyUnit.setSelection(editor, 'p', 0, 'p', 0); editor.formatter.remove('formatA', { value: 'a' }); assert.equal(getContent(editor), '

test

'); }); it('TINY-8036: Remove blockquote format with multiple words and collapsed selection', () => { const editor = hook.editor(); editor.setContent('

test test

'); TinySelections.setCursor(editor, [ 0, 0, 0 ], 5); editor.formatter.remove('blockquote'); TinyAssertions.assertContent(editor, '

test test

'); }); it('TINY-8755: Non-internal attributes are not removed', () => { const editor = hook.editor(); // eslint-disable-next-line max-len editor.setContent('

bold' + ZWSP + 'text

', { format: 'raw' }); TinySelections.setSelection(editor, [ 0, 0, 0 ], 2, [ 0, 0, 2 ], 2); editor.formatter.remove('bold'); // eslint-disable-next-line max-len TinyAssertions.assertRawContent(editor, '

bold' + ZWSP + 'text

'); }); });