diff --git a/src/decouplededitor.js b/src/decouplededitor.js index b30e569..a2e1c1c 100644 --- a/src/decouplededitor.js +++ b/src/decouplededitor.js @@ -14,6 +14,7 @@ import DecoupledEditorUI from './decouplededitorui'; import DecoupledEditorUIView from './decouplededitoruiview'; import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement'; import setDataInElement from '@ckeditor/ckeditor5-utils/src/dom/setdatainelement'; +import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin'; import mix from '@ckeditor/ckeditor5-utils/src/mix'; import { isElement } from 'lodash-es'; import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; @@ -47,6 +48,7 @@ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; * {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`}. * * @mixes module:core/editor/utils/dataapimixin~DataApiMixin + * @mixes module:core/editor/utils/elementapimixin~ElementApiMixin * @implements module:core/editor/editorwithui~EditorWithUI * @extends module:core/editor/editor~Editor */ @@ -68,6 +70,7 @@ export default class DecoupledEditor extends Editor { if ( isElement( sourceElementOrData ) ) { this.sourceElement = sourceElementOrData; + this.secureSourceElement(); } this.data.processor = new HtmlDataProcessor(); @@ -252,6 +255,7 @@ export default class DecoupledEditor extends Editor { } mix( DecoupledEditor, DataApiMixin ); +mix( DecoupledEditor, ElementApiMixin ); function getInitialData( sourceElementOrData ) { return isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData; diff --git a/tests/decouplededitor.js b/tests/decouplededitor.js index eab6fa3..39c3377 100644 --- a/tests/decouplededitor.js +++ b/tests/decouplededitor.js @@ -15,12 +15,14 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold'; import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin'; +import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin'; import RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement'; import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils'; import { describeMemoryUsage, testMemoryUsage } from '@ckeditor/ckeditor5-core/tests/_utils/memory'; import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset'; +import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; import { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils'; const editorData = '

foo bar

'; @@ -47,6 +49,10 @@ describe( 'DecoupledEditor', () => { expect( testUtils.isMixed( DecoupledEditor, DataApiMixin ) ).to.be.true; } ); + it( 'has an Element Interface', () => { + testUtils.isMixed( DecoupledEditor, ElementApiMixin ); + } ); + it( 'creates main root element', () => { expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement ); } ); @@ -125,6 +131,20 @@ describe( 'DecoupledEditor', () => { } ); } ); + // 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', () => { + const sourceElement = document.createElement( 'div' ); + + return DecoupledEditor.create( sourceElement ) + .then( editor => { + expect( () => { + new DecoupledEditor( sourceElement ); // eslint-disable-line no-new + } ).to.throw( CKEditorError, /^editor-source-element-used-more-than-once/ ); + + return editor.destroy(); + } ); + } ); + it( 'throws if initial data is passed in Editor#create and config.initialData is also used', done => { DecoupledEditor.create( '

Hello world!

', { initialData: '

I am evil!

',