From 3ddff95e3b7579f029f88e1073aae0515b3905d3 Mon Sep 17 00:00:00 2001 From: Kamil Piechaczek Date: Tue, 4 Jun 2019 15:48:27 +0200 Subject: [PATCH 1/7] Introduced a check that prevents creating the editor using the same element more than once. --- src/ballooneditor.js | 3 +++ tests/ballooneditor.js | 21 ++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/ballooneditor.js b/src/ballooneditor.js index 44c1df9..3c81a77 100644 --- a/src/ballooneditor.js +++ b/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 ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin'; 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 { isElement } from 'lodash-es'; import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; @@ -70,6 +71,7 @@ export default class BalloonEditor extends Editor { const plugins = this.config.get( 'plugins' ); plugins.push( BalloonToolbar ); + this.config.set( 'plugins', plugins ); this.config.define( 'balloonToolbar', this.config.get( 'toolbar' ) ); @@ -82,6 +84,7 @@ export default class BalloonEditor extends Editor { this.ui = new BalloonEditorUI( this, view ); attachToForm( this ); + secureSourceElement( this ); } /** diff --git a/tests/ballooneditor.js b/tests/ballooneditor.js index acb29fd..681dcba 100644 --- a/tests/ballooneditor.js +++ b/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 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 log from '@ckeditor/ckeditor5-utils/src/log'; @@ -117,6 +118,13 @@ describe( 'BalloonEditor', () => { 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()', () => { @@ -158,6 +166,9 @@ describe( 'BalloonEditor', () => { } ); it( 'should not require config object', () => { + const editorElement = document.createElement( 'div' ); + editorElement.innerHTML = '

foo bar

'; + // Just being safe with `builtinPlugins` static property. class CustomBalloonEditor extends BalloonEditor {} CustomBalloonEditor.builtinPlugins = [ Paragraph, Bold ]; @@ -167,6 +178,9 @@ describe( 'BalloonEditor', () => { expect( newEditor.getData() ).to.equal( '

foo bar

' ); return newEditor.destroy(); + } ) + .then( () => { + editorElement.remove(); } ); } ); @@ -181,13 +195,18 @@ describe( 'BalloonEditor', () => { } ); it( 'initializes with config.initialData', () => { + const editorElement = document.createElement( 'div' ); + editorElement.innerHTML = '

foo bar

'; + return BalloonEditor.create( editorElement, { initialData: '

Hello world!

', plugins: [ Paragraph ] } ).then( editor => { expect( editor.getData() ).to.equal( '

Hello world!

' ); - editor.destroy(); + return editor.destroy(); + } ).then( () => { + editorElement.remove(); } ); } ); From d2d7b084849398d7d20b118f421877f3644d152c Mon Sep 17 00:00:00 2001 From: Aleksander Nowodzinski Date: Fri, 19 Jul 2019 17:05:06 +0200 Subject: [PATCH 2/7] Updated the ElementApiMixin#secureSourceElement error message to the latest API. --- tests/ballooneditor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ballooneditor.js b/tests/ballooneditor.js index d9a119f..b9bc9e2 100644 --- a/tests/ballooneditor.js +++ b/tests/ballooneditor.js @@ -92,7 +92,7 @@ describe( 'BalloonEditor', () => { 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/ ); + } ).to.throw( CKEditorError, /^editor-source-element-used-more-than-once/ ); } ); } ); From 6c6a7f24f9c4485568bbd6076ebd61af03691753 Mon Sep 17 00:00:00 2001 From: Aleksander Nowodzinski Date: Fri, 19 Jul 2019 17:12:15 +0200 Subject: [PATCH 3/7] Used the ElementApiMixin#secureSourceElement(). --- src/ballooneditor.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ballooneditor.js b/src/ballooneditor.js index 800ab36..c815a2f 100644 --- a/src/ballooneditor.js +++ b/src/ballooneditor.js @@ -17,7 +17,6 @@ import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromele import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin'; import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin'; 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 { isElement } from 'lodash-es'; import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; @@ -84,7 +83,7 @@ export default class BalloonEditor extends Editor { this.ui = new BalloonEditorUI( this, view ); attachToForm( this ); - secureSourceElement( this ); + this.secureSourceElement(); } /** From be26019a9053f80e684f717d0ed748bb8c5469a2 Mon Sep 17 00:00:00 2001 From: Aleksander Nowodzinski Date: Fri, 19 Jul 2019 17:14:55 +0200 Subject: [PATCH 4/7] Code refactoring. --- src/ballooneditor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ballooneditor.js b/src/ballooneditor.js index c815a2f..a5e639f 100644 --- a/src/ballooneditor.js +++ b/src/ballooneditor.js @@ -66,6 +66,7 @@ export default class BalloonEditor extends Editor { if ( isElement( sourceElementOrData ) ) { this.sourceElement = sourceElementOrData; + this.secureSourceElement(); } const plugins = this.config.get( 'plugins' ); @@ -83,7 +84,6 @@ export default class BalloonEditor extends Editor { this.ui = new BalloonEditorUI( this, view ); attachToForm( this ); - this.secureSourceElement(); } /** From 6bdbd2870908d884c985773bfabfbcd0259c2bd9 Mon Sep 17 00:00:00 2001 From: Aleksander Nowodzinski Date: Mon, 22 Jul 2019 11:07:45 +0200 Subject: [PATCH 5/7] Used secureSourceElement() as a helper. --- src/ballooneditor.js | 3 ++- tests/ballooneditor.js | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/ballooneditor.js b/src/ballooneditor.js index a5e639f..6f50024 100644 --- a/src/ballooneditor.js +++ b/src/ballooneditor.js @@ -20,6 +20,7 @@ import attachToForm from '@ckeditor/ckeditor5-core/src/editor/utils/attachtoform import mix from '@ckeditor/ckeditor5-utils/src/mix'; import { isElement } from 'lodash-es'; import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; +import secureSourceElement from '@ckeditor/ckeditor5-core/src/editor/utils/securesourceelement'; /** * The {@glink builds/guides/overview#balloon-editor balloon editor} implementation (Medium-like editor). @@ -66,7 +67,7 @@ export default class BalloonEditor extends Editor { if ( isElement( sourceElementOrData ) ) { this.sourceElement = sourceElementOrData; - this.secureSourceElement(); + secureSourceElement( this ); } const plugins = this.config.get( 'plugins' ); diff --git a/tests/ballooneditor.js b/tests/ballooneditor.js index b9bc9e2..afe2d0a 100644 --- a/tests/ballooneditor.js +++ b/tests/ballooneditor.js @@ -19,7 +19,6 @@ 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 CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils'; import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset'; @@ -89,10 +88,20 @@ describe( 'BalloonEditor', () => { } ); // 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, /^editor-source-element-used-more-than-once/ ); + it( 'should throw when trying to create the editor using the same source element more than once', done => { + BalloonEditor.create( editorElement ) + .then( + () => { + expect.fail( 'Balloon editor should not initialize on an element already used by other instance.' ); + }, + err => { + assertCKEditorError( err, + /^editor-source-element-used-more-than-once/ + ); + } + ) + .then( done ) + .catch( done ); } ); } ); From b735cd143909e85111159b85d0f52bdd58c6d2d4 Mon Sep 17 00:00:00 2001 From: Aleksander Nowodzinski Date: Mon, 22 Jul 2019 11:36:54 +0200 Subject: [PATCH 6/7] Tests: Code refactoring. --- tests/ballooneditor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ballooneditor.js b/tests/ballooneditor.js index afe2d0a..e5cde83 100644 --- a/tests/ballooneditor.js +++ b/tests/ballooneditor.js @@ -96,7 +96,7 @@ describe( 'BalloonEditor', () => { }, err => { assertCKEditorError( err, - /^editor-source-element-used-more-than-once/ + /^securesourceelement-element-used-more-than-once/ ); } ) From d6de1a69916a6d6d5c5b14b7eaf7c814063fc460 Mon Sep 17 00:00:00 2001 From: Aleksander Nowodzinski Date: Tue, 23 Jul 2019 08:46:57 +0200 Subject: [PATCH 7/7] Code refactoring: Renamed the error thrown by the secureSourceElement() helper. --- tests/ballooneditor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ballooneditor.js b/tests/ballooneditor.js index e5cde83..277dcec 100644 --- a/tests/ballooneditor.js +++ b/tests/ballooneditor.js @@ -96,7 +96,7 @@ describe( 'BalloonEditor', () => { }, err => { assertCKEditorError( err, - /^securesourceelement-element-used-more-than-once/ + /^editor-source-element-already-used/ ); } )