8
0
Просмотр исходного кода

Introduced a check that prevents creating the editor using the same element more than once.

Kamil Piechaczek 6 лет назад
Родитель
Сommit
20284a14f6

+ 3 - 0
packages/ckeditor5-editor-balloon/src/ballooneditor.js

@@ -17,6 +17,7 @@ import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromele
 import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin';
 import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin';
 import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin';
 import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin';
 import attachToForm from '@ckeditor/ckeditor5-core/src/editor/utils/attachtoform';
 import attachToForm from '@ckeditor/ckeditor5-core/src/editor/utils/attachtoform';
+import secureSourceElement from '@ckeditor/ckeditor5-core/src/editor/utils/securesourceelement';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
 import { isElement } from 'lodash-es';
 import { isElement } from 'lodash-es';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
@@ -70,6 +71,7 @@ export default class BalloonEditor extends Editor {
 
 
 		const plugins = this.config.get( 'plugins' );
 		const plugins = this.config.get( 'plugins' );
 		plugins.push( BalloonToolbar );
 		plugins.push( BalloonToolbar );
+
 		this.config.set( 'plugins', plugins );
 		this.config.set( 'plugins', plugins );
 
 
 		this.config.define( 'balloonToolbar', this.config.get( 'toolbar' ) );
 		this.config.define( 'balloonToolbar', this.config.get( 'toolbar' ) );
@@ -82,6 +84,7 @@ export default class BalloonEditor extends Editor {
 		this.ui = new BalloonEditorUI( this, view );
 		this.ui = new BalloonEditorUI( this, view );
 
 
 		attachToForm( this );
 		attachToForm( this );
+		secureSourceElement( this );
 	}
 	}
 
 
 	/**
 	/**

+ 20 - 1
packages/ckeditor5-editor-balloon/tests/ballooneditor.js

@@ -19,6 +19,7 @@ import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin
 import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin';
 import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin';
 import RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement';
 import RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement';
 
 
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import log from '@ckeditor/ckeditor5-utils/src/log';
 import log from '@ckeditor/ckeditor5-utils/src/log';
 
 
@@ -117,6 +118,13 @@ describe( 'BalloonEditor', () => {
 					return newEditor.destroy();
 					return newEditor.destroy();
 				} );
 				} );
 		} );
 		} );
+
+		// See: https://github.com/ckeditor/ckeditor5/issues/746
+		it( 'should throw when trying to create the editor using the same source element more than once', () => {
+			expect( () => {
+				new BalloonEditor( editorElement, { plugins: [] } ); // eslint-disable-line no-new
+			} ).to.throw( CKEditorError, /^securesourceelement-source-element-used-more-than-once/ );
+		} );
 	} );
 	} );
 
 
 	describe( 'create()', () => {
 	describe( 'create()', () => {
@@ -158,6 +166,9 @@ describe( 'BalloonEditor', () => {
 		} );
 		} );
 
 
 		it( 'should not require config object', () => {
 		it( 'should not require config object', () => {
+			const editorElement = document.createElement( 'div' );
+			editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
+
 			// Just being safe with `builtinPlugins` static property.
 			// Just being safe with `builtinPlugins` static property.
 			class CustomBalloonEditor extends BalloonEditor {}
 			class CustomBalloonEditor extends BalloonEditor {}
 			CustomBalloonEditor.builtinPlugins = [ Paragraph, Bold ];
 			CustomBalloonEditor.builtinPlugins = [ Paragraph, Bold ];
@@ -167,6 +178,9 @@ describe( 'BalloonEditor', () => {
 					expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
 					expect( newEditor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
 
 
 					return newEditor.destroy();
 					return newEditor.destroy();
+				} )
+				.then( () => {
+					editorElement.remove();
 				} );
 				} );
 		} );
 		} );
 
 
@@ -181,13 +195,18 @@ describe( 'BalloonEditor', () => {
 		} );
 		} );
 
 
 		it( 'initializes with config.initialData', () => {
 		it( 'initializes with config.initialData', () => {
+			const editorElement = document.createElement( 'div' );
+			editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
+
 			return BalloonEditor.create( editorElement, {
 			return BalloonEditor.create( editorElement, {
 				initialData: '<p>Hello world!</p>',
 				initialData: '<p>Hello world!</p>',
 				plugins: [ Paragraph ]
 				plugins: [ Paragraph ]
 			} ).then( editor => {
 			} ).then( editor => {
 				expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
 				expect( editor.getData() ).to.equal( '<p>Hello world!</p>' );
 
 
-				editor.destroy();
+				return editor.destroy();
+			} ).then( () => {
+				editorElement.remove();
 			} );
 			} );
 		} );
 		} );