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

Moved ElementApiMixin#secureSourceElement() back to the helper because it does not make sense in the mixin because the mixin is used wrongly in the first place :P

Aleksander Nowodzinski 6 лет назад
Родитель
Сommit
984114ce81

+ 0 - 37
packages/ckeditor5-core/src/editor/utils/elementapimixin.js

@@ -37,43 +37,6 @@ const ElementApiMixin = {
 		}
 
 		setDataInElement( this.sourceElement, this.data.get() );
-	},
-
-	/**
-	 * Marks the source element the editor was initialized on preventing other editor instances from
-	 * using this element.
-	 *
-	 * Running multiple editor instances on the same source element causes various issues and it is
-	 * crucial this helper is called as soon as the source element is known to prevent collisions.
-	 */
-	secureSourceElement() {
-		const sourceElement = this.sourceElement;
-
-		// If the editor was initialized without specifying an element, we don't need to secure anything.
-		if ( !sourceElement ) {
-			return;
-		}
-
-		if ( sourceElement.dataset.ckeditorSecuredElement ) {
-			/**
-			 * A DOM element used to create the editor (e.g.
-			 * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`})
-			 * has already been used to create another editor instance. Make sure each editor is
-			 * created with an unique DOM element.
-			 *
-			 * @error editor-source-element-used-more-than-once
-			 */
-			throw new CKEditorError(
-				'editor-source-element-used-more-than-once: ' +
-				'The DOM source element cannot be used to create an editor more than once.'
-			);
-		}
-
-		sourceElement.dataset.ckeditorSecuredElement = true;
-
-		this.once( 'destroy', () => {
-			delete sourceElement.dataset.ckeditorSecuredElement;
-		} );
 	}
 };
 

+ 49 - 0
packages/ckeditor5-core/src/editor/utils/securesourceelement.js

@@ -0,0 +1,49 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
+
+/**
+ * @module core/editor/utils/securesourceelement
+ */
+
+/**
+ * Marks the source element the editor was initialized on preventing other editor instances from
+ * using this element.
+ *
+ * Running multiple editor instances on the same source element causes various issues and it is
+ * crucial this helper is called as soon as the source element is known to prevent collisions.
+ *
+ * @param {module:core/editor/editor~Editor} editor Editor instance.
+ */
+export default function secureSourceElement( editor ) {
+	const sourceElement = editor.sourceElement;
+
+	// If the editor was initialized without specifying an element, we don't need to secure anything.
+	if ( !sourceElement ) {
+		return;
+	}
+
+	if ( sourceElement.dataset.ckeditorSecuredElement ) {
+		/**
+		 * A DOM element used to create the editor (e.g.
+		 * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`})
+		 * has already been used to create another editor instance. Make sure each editor is
+		 * created with an unique DOM element.
+		 *
+		 * @error editor-source-element-used-more-than-once
+		 */
+		throw new CKEditorError(
+			'editor-source-element-used-more-than-once: ' +
+			'The DOM source element cannot be used to create an editor more than once.'
+		);
+	}
+
+	sourceElement.dataset.ckeditorSecuredElement = true;
+
+	editor.once( 'destroy', () => {
+		delete sourceElement.dataset.ckeditorSecuredElement;
+	} );
+}

+ 0 - 48
packages/ckeditor5-core/tests/editor/utils/elementapimixin.js

@@ -53,52 +53,4 @@ describe( 'ElementApiMixin', () => {
 			);
 		} );
 	} );
-
-	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/ );
-		} );
-	} );
 } );

+ 67 - 0
packages/ckeditor5-core/tests/editor/utils/securesourceelement.js

@@ -0,0 +1,67 @@
+/**
+ * @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 secureSourceElement from '../../../src/editor/utils/securesourceelement';
+import Editor from '../../../src/editor/editor';
+import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
+
+describe( 'secureSourceElement()', () => {
+	let editor, sourceElement;
+
+	beforeEach( () => {
+		class CustomEditor extends Editor {}
+
+		sourceElement = document.createElement( 'div' );
+		editor = new CustomEditor();
+
+		editor.sourceElement = sourceElement;
+		editor.state = 'ready';
+	} );
+
+	afterEach( () => {
+		if ( editor ) {
+			return editor.destroy();
+		}
+	} );
+
+	it( 'does not throw if the editor was not initialized using the source element', () => {
+		delete editor.sourceElement;
+
+		expect( () => {
+			secureSourceElement( editor );
+		} ).to.not.throw();
+	} );
+
+	it( 'does not throw if the editor was initialized using the element for the first time', () => {
+		expect( () => {
+			secureSourceElement( editor );
+		} ).to.not.throw();
+	} );
+
+	it( 'sets the data attribute after initializing the editor', () => {
+		secureSourceElement( editor );
+
+		expect( sourceElement.dataset.ckeditorSecuredElement ).to.equal( 'true' );
+	} );
+
+	it( 'removes the data attribute after destroying the editor', () => {
+		secureSourceElement( editor );
+
+		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(
+			() => secureSourceElement( editor ),
+			/^editor-source-element-used-more-than-once/ );
+	} );
+} );