This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
elementapimixin.js
104 lines (79 loc) · 2.87 KB
/
elementapimixin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/* global document */
import ElementApiMixin from '../../../src/editor/utils/elementapimixin';
import Editor from '../../../src/editor/editor';
import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
import mix from '@ckeditor/ckeditor5-utils/src/mix';
import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
describe( 'ElementApiMixin', () => {
let editor;
beforeEach( () => {
class CustomEditor extends Editor {}
mix( CustomEditor, ElementApiMixin );
editor = new CustomEditor();
editor.data.processor = new HtmlDataProcessor();
editor.model.document.createRoot();
editor.model.schema.extend( '$text', { allowIn: '$root' } );
} );
afterEach( () => {
editor.destroy();
} );
describe( 'updateSourceElement()', () => {
it( 'should be added to editor interface', () => {
expect( editor ).have.property( 'updateSourceElement' ).to.be.a( 'function' );
} );
it( 'sets data to editor element', () => {
const editorElement = document.createElement( 'div' );
editor.data.set( 'foo bar' );
editor.sourceElement = editorElement;
editor.updateSourceElement();
expect( editorElement.innerHTML ).to.equal( 'foo bar' );
} );
it( 'throws an error if "sourceElement" has not been set', () => {
expectToThrowCKEditorError(
() => editor.updateSourceElement(),
/editor-missing-sourceelement/,
editor
);
} );
} );
describe( 'secureSourceElement()', () => {
let sourceElement;
beforeEach( () => {
sourceElement = document.createElement( 'div' );
editor.sourceElement = sourceElement;
editor.state = 'ready';
} );
it( 'does not throw if the editor was not initialized using the source element', () => {
delete editor.sourceElement;
expect( () => {
editor.secureSourceElement();
} ).to.not.throw();
} );
it( 'does not throw if the editor was initialized using the element for the first time', () => {
expect( () => {
editor.secureSourceElement();
} ).to.not.throw();
} );
it( 'sets the data attribute after initializing the editor', () => {
editor.secureSourceElement();
expect( sourceElement.dataset.ckeditorSecuredElement ).to.equal( 'true' );
} );
it( 'removes the data attribute after destroying the editor', () => {
editor.secureSourceElement();
return editor.destroy()
.then( () => {
expect( sourceElement.dataset.ckeditorSecuredElement ).to.be.undefined;
} );
} );
it( 'throws an error if the same element was used twice', () => {
sourceElement.dataset.ckeditorSecuredElement = true;
expectToThrowCKEditorError(
() => editor.secureSourceElement(),
/^editor-source-element-used-more-than-once/ );
} );
} );
} );