import { Assertions } from '@ephox/agar'; import { describe, it } from '@ephox/bedrock-client'; import { Obj } from '@ephox/katamari'; import { LegacyUnit, TinyAssertions, TinyHooks, TinySelections } from '@ephox/wrap-mcagar'; import { assert } from 'chai'; import Editor from 'tinymce/core/api/Editor'; import * as KeyUtils from '../module/test/KeyUtils'; describe('browser.tinymce.core.FormatterApplyTest', () => { const hook = TinyHooks.bddSetupLight({ indent: false, extended_valid_elements: 'b[id|style|title],i[id|style|title],span[id|class|style|title|contenteditable],font[face|size]', entities: 'raw', convert_fonts_to_spans: false, 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' }, [], true); const getContent = (editor: Editor) => { return editor.getContent().toLowerCase().replace(/[\r]+/g, ''); }; it('apply inline to a list', () => { const editor = hook.editor(); editor.focus(); editor.formatter.register('format', { inline: 'b', toggle: false }); editor.getBody().innerHTML = '

1234

5678

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

1234

5678

', 'selection of a list' ); }); it('Toggle OFF - Inline element on selected text', () => { const editor = hook.editor(); // Toggle OFF - Inline element on selected text editor.formatter.register('format', { inline: 'b', toggle: false }); 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.toggle('format'); assert.equal(getContent(editor), '

1234

'); }); it('Toggle OFF - Inline element on partially selected text', () => { const editor = hook.editor(); // Toggle OFF - Inline element on partially selected text editor.formatter.register('format', { inline: 'b', toggle: false }); 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, 2); editor.selection.setRng(rng); editor.formatter.toggle('format'); assert.equal(getContent(editor), '

1234

'); }); it('Toggle OFF - Inline element on partially selected text in start/end elements', () => { const editor = hook.editor(); // Toggle OFF - Inline element on partially selected text in start/end elements editor.formatter.register('format', { inline: 'b', toggle: false }); editor.getBody().innerHTML = '

1234

1234

'; // '

1234

1234

'; 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.toggle('format'); assert.equal(getContent(editor), '

1234

1234

'); }); it('Toggle OFF - Inline element with data attribute', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

1

'; 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, 1); editor.selection.setRng(rng); editor.formatter.toggle('format'); assert.equal(getContent(editor), '

1

'); }); it('Toggle ON - NO inline element on selected text', () => { const editor = hook.editor(); // Inline element on selected text editor.formatter.register('format', { inline: 'b', toggle: true }); editor.getBody().innerHTML = '

1234

'; 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.apply('format'); assert.equal(getContent(editor), '

1234

', 'Inline element on selected text'); editor.formatter.toggle('format'); assert.equal(getContent(editor), '

1234

', 'Toggle ON - NO inline element on selected text'); }); it('Selection spanning from within format to outside format with toggle off', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b', toggle: false }); 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('p')[0].lastChild as Text, 2); editor.selection.setRng(rng); editor.formatter.toggle('format'); assert.equal(getContent(editor), '

1234

', 'Extend formating if start of selection is already formatted'); }); it('Inline element on partially selected 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('p')[0].firstChild as Text, 1); rng.setEnd(editor.dom.select('p')[0].firstChild as Text, 3); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '

1234

', 'Inline element on partially selected text'); editor.formatter.toggle('format'); assert.equal(getContent(editor), '

1234

', 'Toggle ON - NO inline element on partially selected text'); }); it('Inline element on partially selected text in start/end elements', () => { const editor = hook.editor(); // Inline element on partially selected text in start/end elements editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

1234

1234

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

1234

1234

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

1234

'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody(), 0); rng.setEnd(editor.getBody(), 1); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '

1234

', 'Inline element on selected element'); }); it('Inline element on multiple selected elements', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

1234

1234

'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody(), 0); rng.setEnd(editor.getBody(), 2); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '

1234

1234

', 'Inline element on multiple selected elements'); }); it('Inline element on multiple selected elements with various childnodes', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

123456789

123456789

'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody(), 0); rng.setEnd(editor.getBody(), 2); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal( getContent(editor), '

123456789

123456789

', 'Inline element on multiple selected elements with various childnodes' ); }); it('Inline element with attributes', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b', attributes: { title: 'value1', id: 'value2' } }); editor.getBody().innerHTML = '

1234

'; 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.apply('format'); assert.equal(getContent(editor), '

1234

', 'Inline element with attributes'); }); it('Inline element with styles', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b', styles: { color: '#ff0000', fontSize: '10px' } }); editor.getBody().innerHTML = '

1234

'; 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.apply('format'); assert.equal(getContent(editor), '

1234

', 'Inline element with styles'); }); it('Inline element with attributes and styles', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b', attributes: { title: 'value1', id: 'value2' }, styles: { color: '#ff0000', fontSize: '10px' } }); editor.getBody().innerHTML = '

1234

'; 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.apply('format'); assert.equal( getContent(editor), '

1234

', 'Inline element with attributes and styles' ); }); it('Inline element with wrapable parents', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

x1234y

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

x1234y

', 'Inline element with wrapable parents'); }); it('Inline element with redundant child', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); 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.apply('format'); assert.equal(getContent(editor), '

1234

', 'Inline element with redundant child'); }); it('Inline element with redundant parent', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

a1234b

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

a1234b

', 'Inline element with redundant parent'); }); it('Inline element with redundant child of similar type 1', () => { const editor = hook.editor(); editor.formatter.register('format', [{ inline: 'b' }, { inline: 'strong' }]); editor.getBody().innerHTML = '

a1234b

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

a1234b

', 'Inline element with redundant child of similar type 1'); }); it('Inline element with redundant child of similar type 2', () => { const editor = hook.editor(); editor.formatter.register('format', [{ inline: 'b' }, { inline: 'span', styles: { fontWeight: 'bold' } }]); 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.apply('format'); assert.equal(getContent(editor), '

1234

', 'Inline element with redundant child of similar type 2'); }); it('Inline element with redundant children of similar types', () => { const editor = hook.editor(); editor.formatter.register('format', [{ inline: 'b' }, { inline: 'strong' }, { inline: 'span', styles: { fontWeight: 'bold' } }]); editor.getBody().innerHTML = '

a12345678b

'; 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.apply('format'); assert.equal(getContent(editor), '

a12345678b

', 'Inline element with redundant children of similar types'); }); it('Inline element with redundant parent 1', () => { const editor = hook.editor(); editor.formatter.register('format', [{ inline: 'b' }, { inline: 'strong' }]); editor.getBody().innerHTML = '

a1234b

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

a1234b

', 'Inline element with redundant parent 1'); }); it('Inline element with redundant parent 2', () => { const editor = hook.editor(); editor.formatter.register('format', [{ inline: 'b' }, { inline: 'span', styles: { fontWeight: 'bold' } }]); editor.getBody().innerHTML = '

a1234b

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

a1234b

', 'Inline element with redundant parent 2'); }); it('Inline element with redundant parents of similar types', () => { const editor = hook.editor(); editor.formatter.register('format', [{ inline: 'b' }, { inline: 'strong' }, { inline: 'span', styles: { fontWeight: 'bold' } }]); editor.getBody().innerHTML = '

a1234b

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

a1234b

', 'Inline element with redundant parents of similar types' ); }); it('Inline element merged with parent and child', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

a123456b

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

a123456b

', 'Inline element merged with parent and child'); }); it('Inline element merged with child 1', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'span', styles: { fontWeight: 'bold' } }); editor.getBody().innerHTML = '

a1234b

'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody(), 0); rng.setEnd(editor.getBody(), 1); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '

a1234b

', 'Inline element merged with child 1'); }); it('Inline element merged with child 2', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'span', styles: { fontWeight: 'bold' } }); editor.getBody().innerHTML = '

a1234b

'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody(), 0); rng.setEnd(editor.getBody(), 1); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal( getContent(editor), '

a1234b

', 'Inline element merged with child 2' ); }); it('Inline element merged with child 3', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'span', styles: { fontWeight: 'bold' } }); editor.getBody().innerHTML = '

a1234b

'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody(), 0); rng.setEnd(editor.getBody(), 1); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal( getContent(editor), '

a1234b

', 'Inline element merged with child 3' ); }); it('Inline element merged with child 4', () => { 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.getBody(), 0); rng.setEnd(editor.getBody(), 1); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '

1234

'); }); it('Inline element merged with child 5', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'span', styles: { color: '#00ff00' } }); editor.getBody().innerHTML = '

1234

'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody(), 0); rng.setEnd(editor.getBody(), 1); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '

1234

', 'Inline element merged with child 5'); }); it('Inline element with attributes merged with child 1', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'font', attributes: { face: 'arial' } }); editor.getBody().innerHTML = '

1234

'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody(), 0); rng.setEnd(editor.getBody(), 1); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '

1234

', 'Inline element with attributes merged with child 1'); }); it('Inline element with attributes merged with child 2', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'font', attributes: { size: '7' } }); editor.getBody().innerHTML = '

a1234b

'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody(), 0); rng.setEnd(editor.getBody(), 1); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '

a1234b

', 'Inline element with attributes merged with child 2'); }); it('Inline element merged with left sibling', () => { 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].lastChild as Text, 0); rng.setEnd(editor.dom.select('p')[0].lastChild as Text, 4); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '

12345678

', 'Inline element merged with left sibling'); }); it('Inline element merged with right sibling', () => { 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].firstChild as Text, 0); rng.setEnd(editor.dom.select('p')[0].firstChild as Text, 4); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '

12345678

', 'Inline element merged with right sibling'); }); it('Inline element merged with left and right siblings', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

123456

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

123456

', 'Inline element merged with left and right siblings'); }); it('Inline element merged with data attributed left sibling', () => { 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].lastChild as Text, 0); rng.setEnd(editor.dom.select('p')[0].lastChild as Text, 4); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '

12345678

', 'Inline element merged with left sibling'); }); it(`Don't merge siblings with whitespace between 1`, () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

a b

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('p')[0].lastChild as Text, 1); rng.setEnd(editor.dom.select('p')[0].lastChild as Text, 2); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '

a b

', `Don't merge siblings with whitespace between 1`); }); it(`Don't merge siblings with whitespace between 2`, () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

a b

'; 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, 1); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '

a b

', `Don't merge siblings with whitespace between 2`); }); it('Inline element not merged in exact mode', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'span', styles: { color: '#00ff00' }, exact: true }); editor.getBody().innerHTML = '

1234

'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody(), 0); rng.setEnd(editor.getBody(), 1); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal( getContent(editor), '

1234

', 'Inline element not merged in exact mode' ); }); it('Inline element merged in exact mode', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'span', styles: { color: '#ff0000' }, exact: true }); editor.getBody().innerHTML = '

1234

'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody(), 0); rng.setEnd(editor.getBody(), 1); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '

1234

'); }); it('Deep left branch', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

1234text1text2

5678

9012

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

1234text1text2

5678

9012

', 'Deep left branch' ); }); it('Deep right branch', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

9012

5678

1234text1text2

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

9012

5678

1234text1text2

', 'Deep right branch' ); }); it('Full element text selection on two elements with a table in the middle', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b' }); editor.getBody().innerHTML = '

1234

123

5678

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

1234

123

5678

', 'Full element text selection on two elements with a table in the middle' ); }); it('Inline element on selected text with variables', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b', styles: { color: '%color' }, attributes: { title: '%title' } }); editor.getBody().innerHTML = '

1234

'; 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.apply('format', { color: '#ff0000', title: 'title' }); assert.equal(getContent(editor), '

1234

', 'Inline element on selected text'); }); it('Remove redundant children', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'span', styles: { fontFamily: 'arial' } }); 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.apply('format'); assert.equal(getContent(editor), '

1234

', 'Remove redundant children'); }); it('Inline element on selected text with function values', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'b', styles: { color: (vars) => { return vars?.color + '00ff'; } }, attributes: { title: (vars) => { return vars?.title + '2'; } } }); editor.getBody().innerHTML = '

1234

'; 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.apply('format', { color: '#ff', title: 'title' }); assert.equal(getContent(editor), '

1234

', 'Inline element on selected text with function values'); }); it('Block element on selected text', () => { const editor = hook.editor(); editor.formatter.register('format', { block: 'div' }); editor.getBody().innerHTML = '

1234

'; 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.apply('format'); assert.equal(getContent(editor), '
1234
', 'Block element on selected text'); }); it('Block element on partially selected text', () => { const editor = hook.editor(); editor.formatter.register('format', { block: 'div' }); editor.getBody().innerHTML = '

1234

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('p')[0].firstChild as Text, 1); rng.setEnd(editor.dom.select('p')[0].firstChild as Text, 3); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '
1234
', 'Block element on partially selected text'); }); it('Block element on selected element', () => { const editor = hook.editor(); editor.formatter.register('format', { block: 'div' }); editor.getBody().innerHTML = '

1234

'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody(), 0); rng.setEnd(editor.getBody(), 1); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '
1234
', 'Block element on selected element'); }); it('Block element on selected elements', () => { const editor = hook.editor(); editor.formatter.register('format', { block: 'div' }); editor.getBody().innerHTML = '

1234

5678

'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody(), 0); rng.setEnd(editor.getBody(), 2); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '
1234
5678
', 'Block element on selected elements'); }); it('Block element on selected elements with attributes', () => { const editor = hook.editor(); editor.formatter.register('format', { block: 'div', attributes: { title: 'test' } }); editor.getBody().innerHTML = '

1234

5678

'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody(), 0); rng.setEnd(editor.getBody(), 2); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '
1234
5678
', 'Block element on selected elements with attributes'); }); it('Block element on nested element', () => { const editor = hook.editor(); editor.formatter.register('format', { block: 'p' }); editor.getBody().innerHTML = '

1234

'; 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.apply('format'); assert.equal(getContent(editor), '

1234

', 'Block element on nested element'); }); it('Block element on selected non wrapped text 1', () => { const editor = hook.editor(); editor.formatter.register('format', { block: 'div' }); editor.getBody().innerHTML = '1234'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody().firstChild as Text, 0); rng.setEnd(editor.getBody().firstChild as Text, 4); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '
1234
', 'Block element on selected non wrapped text 1'); }); it('Block element on selected non wrapped text 2', () => { const editor = hook.editor(); editor.formatter.register('format', { block: 'div' }); editor.getBody().innerHTML = '1234
4567
8910'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody().firstChild as Text, 0); rng.setEnd(editor.getBody().lastChild as Text, 4); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '
1234
4567
8910
', 'Block element on selected non wrapped text 2'); }); it('Block element on selected non wrapped text 3', () => { const editor = hook.editor(); editor.formatter.register('format', { block: 'div' }); editor.getBody().innerHTML = '
1234

4567
8910
'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody(), 0); rng.setEnd(editor.getBody(), 7); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '
1234
4567
8910
', 'Block element on selected non wrapped text 3'); }); it('Block element wrapper 1', () => { const editor = hook.editor(); editor.formatter.register('format', { block: 'blockquote', wrapper: true }); editor.getBody().innerHTML = '

1234

5678

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

1234

5678

', 'Block element wrapper 1'); }); it('Block element wrapper 2', () => { const editor = hook.editor(); editor.formatter.register('format', { block: 'blockquote', wrapper: true }); editor.getBody().innerHTML = '

1234

'; 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.apply('format'); assert.equal(getContent(editor), '

1234

', 'Block element wrapper 2'); }); it('Block element wrapper 3', () => { const editor = hook.editor(); editor.formatter.register('format', { block: 'blockquote', wrapper: true }); editor.getBody().innerHTML = '

1234


'; const rng = editor.dom.createRng(); rng.setStart(editor.getBody(), 0); rng.setEnd(editor.getBody(), 3); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal(getContent(editor), '

1234

', 'Block element wrapper 3'); }); it('Apply format on single element that matches a selector 1', () => { const editor = hook.editor(); editor.formatter.register('format', { selector: 'p', attributes: { title: 'test' }, styles: { color: '#ff0000' }, classes: 'a b c' }); editor.getBody().innerHTML = '

1234

'; 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.apply('format'); assert.equal( getContent(editor), '

1234

', 'Apply format on single element that matches a selector' ); }); it('Apply format on single element parent that matches a selector 2', () => { const editor = hook.editor(); editor.formatter.register('format', { selector: 'div', attributes: { title: 'test' }, styles: { color: '#ff0000' }, classes: 'a b c' }); editor.getBody().innerHTML = '

1234

test

1234

'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('div')[0], 1); rng.setEnd(editor.dom.select('div')[0], 2); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal( getContent(editor), '

1234

test

1234

', 'Apply format on single element parent that matches a selector' ); }); it('Apply format on multiple elements that matches a selector 2', () => { const editor = hook.editor(); editor.formatter.register('format', { selector: 'p', attributes: { title: 'test' }, styles: { color: '#ff0000' }, classes: 'a b c' }); editor.getBody().innerHTML = '

1234

test

1234

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

1234

test

1234

', 'Apply format on multiple elements that matches a selector' ); }); it('Apply format on top of existing selector element', () => { const editor = hook.editor(); editor.formatter.register('format', { selector: 'p', attributes: { title: 'test2' }, styles: { color: '#00ff00' }, classes: 'a b c' }); editor.getBody().innerHTML = '

1234

'; 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.apply('format'); assert.equal( getContent(editor), '

1234

', 'Apply format on top of existing selector element' ); }); it('Format on single li that matches a selector', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'span', selector: 'li', attributes: { title: 'test' }, styles: { color: '#ff0000' }, classes: 'a b c' }); editor.getBody().innerHTML = '
text
'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('div')[0], 0); rng.setEnd(editor.dom.select('div')[0], 1); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal( getContent(editor), '
text
', 'Apply format on single element that matches a selector' ); }); it('Format on single div that matches a selector', () => { const editor = hook.editor(); editor.formatter.register('format', { inline: 'span', selector: 'div', attributes: { title: 'test' }, styles: { color: '#ff0000' }, classes: 'a b c' }); editor.getBody().innerHTML = '
text
'; const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('div')[0], 0); rng.setEnd(editor.dom.select('div')[0], 1); editor.selection.setRng(rng); editor.formatter.apply('format'); assert.equal( getContent(editor), '
text
', 'Apply format on single element that matches a selector' ); }); it('Bold and italics is applied to text that is not highlighted', () => { const editor = hook.editor(); const rng = editor.dom.createRng(); editor.setContent('

test1 test2 test3 test4 test5 test6

'); rng.setStart(editor.dom.select('strong')[0].firstChild as Text, 6); rng.setEnd(editor.dom.select('strong')[0].firstChild as Text, 11); editor.focus(); editor.selection.setRng(rng); editor.execCommand('Italic'); assert.equal( editor.getContent(), '

test1 test2 test3 test4 test5 test6

', 'Selected text should be bold.' ); }); it('Apply color format to links as well', () => { const editor = hook.editor(); editor.setContent('

123abc456

'); const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('p')[0].firstChild as Text, 0); rng.setEnd(editor.dom.select('p')[0].lastChild as Text, 3); editor.selection.setRng(rng); editor.formatter.register('format', { inline: 'span', styles: { color: '#FF0000' }, links: true }); editor.formatter.apply('format'); assert.equal( editor.getContent(), '

123abc456

', `Link should have it's own color.` ); }); it('Color on link element', () => { const editor = hook.editor(); editor.setContent('

123abc456

'); const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('span')[0].firstChild as Text, 0); rng.setEnd(editor.dom.select('span')[0].lastChild as Text, 3); editor.selection.setRng(rng); editor.formatter.register('format', { inline: 'span', styles: { color: '#FF0000' }, links: true }); editor.formatter.apply('format'); assert.equal( editor.getContent(), '

123abc456

', `Link should have it's own color.` ); }); it('Applying formats in lists', () => { const editor = hook.editor(); editor.setContent(''); const rng = editor.dom.createRng(); rng.setStart(editor.dom.select('li')[0].firstChild as Text, 0); rng.setEnd(editor.dom.select('li')[0].firstChild as Text, 1); editor.selection.setRng(rng); editor.formatter.apply('h1'); assert.equal( editor.getContent(), '', 'heading should not automatically apply to sublists' ); }); it('Applying block format to first character in li', () => { const editor = hook.editor(); editor.setContent('