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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from ckeditor/t/ckeditor5/746
Feature: Introduced a `secureSourceElement()` utility that prevents from initialising more than one editor on the same DOM element. See ckeditor/ckeditor5#746.
- Loading branch information
Showing
4 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @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 on which the editor was initialized. This prevents 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.ckeditorInstance ) { | ||
/** | ||
* A DOM element used to create the editor (e.g. | ||
* {@link module:editor-inline/inlineeditor~InlineEditor.create `InlineEditor.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-already-used | ||
* @param {HTMLElement} element DOM element that caused the collision. | ||
*/ | ||
throw new CKEditorError( | ||
'editor-source-element-already-used: ' + | ||
'The DOM element cannot be used to create multiple editor instances.', | ||
editor | ||
); | ||
} | ||
|
||
sourceElement.ckeditorInstance = editor; | ||
|
||
editor.once( 'destroy', () => { | ||
delete sourceElement.ckeditorInstance; | ||
} ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 property after initializing the editor', () => { | ||
secureSourceElement( editor ); | ||
|
||
expect( sourceElement.ckeditorInstance ).to.equal( editor ); | ||
} ); | ||
|
||
it( 'removes the property after destroying the editor', () => { | ||
secureSourceElement( editor ); | ||
|
||
return editor.destroy() | ||
.then( () => { | ||
expect( sourceElement.ckeditorInstance ).to.be.undefined; | ||
} ); | ||
} ); | ||
|
||
it( 'throws an error if the same element was used twice', () => { | ||
sourceElement.ckeditorInstance = 'foo'; | ||
|
||
expectToThrowCKEditorError( () => { | ||
secureSourceElement( editor ); | ||
}, /^editor-source-element-already-used/, editor ); | ||
} ); | ||
} ); |