import { ApproxStructure } from '@ephox/agar'; import { describe, it } from '@ephox/bedrock-client'; import { TinyAssertions, TinyHooks, TinySelections } from '@ephox/wrap-mcagar'; import Editor from 'tinymce/core/api/Editor'; import { annotate, assertHtmlContent } from '../../module/test/AnnotationAsserts'; describe('browser.tinymce.core.annotate.AnnotateTest', () => { const hook = TinyHooks.bddSetupLight({ base_url: '/project/tinymce/js/tinymce', setup: (ed: Editor) => { ed.on('init', () => { ed.annotator.register('test-annotation', { decorate: (uid, data) => ({ attributes: { 'data-test-anything': data.anything }, classes: [ ] }) }); }); } }, [], true); // TODO: Consider testing collapse sections. it('should word grab with a collapsed selection', () => { const editor = hook.editor(); // '

This |is| the first paragraph

This is the second.

' editor.setContent('

This is the first paragraph here

This is the second.

'); TinySelections.setSelection(editor, [ 0, 0 ], 'This is the first p'.length, [ 0, 0 ], 'This is the first p'.length); annotate(editor, 'test-annotation', 'test-uid', { anything: 'one-paragraph' }); assertHtmlContent(editor, [ '

This is the first paragraph here

', '

This is the second.

' ]); TinyAssertions.assertSelection(editor, [ 0 ], 1, [ 0 ], 2); }); it('should be able to annotate a direct parent of the body (e.g. an empty paragraph)', () => { const editor = hook.editor(); editor.setContent('

First


Third

'); TinySelections.setSelection(editor, [ 1 ], 0, [ 1 ], 0); annotate(editor, 'test-annotation', 'test-uid', { anything: 'empty-paragraph' }); assertHtmlContent(editor, [ '

First

', '


', '

Third

' ]); }); it('should annotate when the cursor is collapsed before two nbsps', () => { const editor = hook.editor(); editor.setContent('

Annotation here   , please

'); TinySelections.setCursor(editor, [ 0, 0 ], 'Annotation here '.length); annotate(editor, 'test-annotation', 'test-uid', { anything: 'nbsp-paragraph' }); TinyAssertions.assertContentStructure( editor, ApproxStructure.build((s, str, arr) => s.element('body', { children: [ s.element('p', { children: [ s.text(str.is('Annotation here ')), s.element('span', { classes: [ arr.has('mce-annotation') ], html: str.is(' ') }), s.text(str.is('\u00A0\u00A0, please')) ] }) ] })) ); }); it('should annotate when the cursor is collapsed between two nbsps', () => { const editor = hook.editor(); editor.setContent('

Annotation here   , please

'); TinySelections.setCursor(editor, [ 0, 0 ], 'Annotation here '.length); annotate(editor, 'test-annotation', 'test-uid', { anything: 'nbsp-paragraph' }); TinyAssertions.assertContentStructure( editor, ApproxStructure.build((s, str, arr) => s.element('body', { children: [ s.element('p', { children: [ s.text( str.is('Annotation here \u00A0') ), s.element('span', { classes: [ arr.has('mce-annotation') ], html: str.is(' ') }), s.text( str.is('\u00A0, please')) ] }) ] })) ); }); it('should annotate when the cursor is collapsed after two nbsps', () => { const editor = hook.editor(); editor.setContent('

Annotation here   , please

'); TinySelections.setCursor(editor, [ 0, 0 ], 'Annotation here '.length); annotate(editor, 'test-annotation', 'test-uid', { anything: 'nbsp-paragraph' }); TinyAssertions.assertContentStructure( editor, ApproxStructure.build((s, str, arr) => s.element('body', { children: [ s.element('p', { children: [ s.text(str.is('Annotation here \u00A0\u00A0')), s.element('span', { classes: [ arr.has('mce-annotation') ], html: str.is(',') }), s.text(str.is(' please')) ] }) ] })) ); }); it('should not word grab if the selection is not collapsed', () => { const editor = hook.editor(); // '

This |is| the first paragraph

This is the second.

' editor.setContent('

This is the first paragraph

This is the second.

'); TinySelections.setSelection(editor, [ 0, 0 ], 'This is the first p'.length, [ 0, 0 ], 'This is the first par'.length); annotate(editor, 'test-annotation', 'test-uid', { anything: 'one-paragraph' }); assertHtmlContent(editor, [ '

This is the first paragraph

', '

This is the second.

' ]); TinyAssertions.assertSelection(editor, [ 0 ], 1, [ 0 ], 2); }); it('testing in one paragraph', () => { const editor = hook.editor(); // '

This |is| the first paragraph

This is the second.

' editor.setContent('

This is the first paragraph

This is the second.

'); TinySelections.setSelection(editor, [ 0, 0 ], 'This '.length, [ 0, 0 ], 'This is'.length); annotate(editor, 'test-annotation', 'test-uid', { anything: 'one-paragraph' }); assertHtmlContent(editor, [ '

This is the first paragraph

', '

This is the second.

' ]); TinyAssertions.assertSelection(editor, [ 0 ], 1, [ 0 ], 2); }); it('testing over two paragraphs', () => { const editor = hook.editor(); // '

This |is the first paragraph

This is| the second.

' editor.setContent('

This is the first paragraph

This is the second.

'); TinySelections.setSelection(editor, [ 0, 0 ], 'This '.length, [ 1, 0 ], 'This is'.length); annotate(editor, 'test-annotation', 'test-uid', { anything: 'two-paragraphs' }); assertHtmlContent(editor, [ '

This is the first paragraph

', '

This is the second.

' ]); TinyAssertions.assertSelection(editor, [ 0 ], 1, [ 1 ], 1); }); it('testing over three paragraphs', () => { const editor = hook.editor(); // '

This |is the first paragraph

This is the second.

This is| the third.

' editor.setContent('

This is the first paragraph

This is the second.

This is the third.

'); TinySelections.setSelection(editor, [ 0, 0 ], 'This '.length, [ 2, 0 ], 'This is'.length); annotate(editor, 'test-annotation', 'test-uid', { anything: 'three-paragraphs' }); assertHtmlContent(editor, [ '

This is the first paragraph

', '

This is the second.

', '

This is the third.

' ]); TinyAssertions.assertSelection(editor, [ 0 ], 1, [ 2 ], 1); }); it('testing in table', () => { const editor = hook.editor(); // '
|cell 1|cell 2
|cell 3|cell 4

This is the second.

' editor.setContent('
cell 1cell 2
cell 3cell 4

This is the second.

'); TinySelections.setSelection(editor, [ 0, 0, 0, 0, 0 ], 0, [ 0, 0, 0, 0, 0 ], 1); annotate(editor, 'test-annotation', 'test-uid', { anything: 'table' }); assertHtmlContent(editor, [ [ '', '', '', '
cell 1cell 2
cell 3cell 4
' ].join(''), '

This is the second.

' ], true); TinyAssertions.assertSelection(editor, [ 0, 0, 0, 0, 0 ], 0, [ 0, 0, 0, 0, 0, 0 ], 1); }); });