Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit 56c9f40

Browse files
authored
Merge pull request #52 from ckeditor/t/ckeditor5/1591
Feature: `InlineEditor.create()` will throw an error, when textarea element is used.
2 parents 6e70f06 + c3ab7f8 commit 56c9f40

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

src/inlineeditor.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ export default class InlineEditor extends Editor {
189189
*/
190190
static create( sourceElementOrData, config = {} ) {
191191
return new Promise( resolve => {
192+
const isHTMLElement = isElement( sourceElementOrData );
193+
194+
if ( isHTMLElement && sourceElementOrData.tagName === 'TEXTAREA' ) {
195+
// Documented in core/editor/editor.js
196+
throw new CKEditorError( 'editor-wrong-element: This type of editor cannot be initialized inside <textarea> element.' );
197+
}
198+
192199
const editor = new this( sourceElementOrData, config );
193200

194201
resolve(
@@ -197,7 +204,7 @@ export default class InlineEditor extends Editor {
197204
editor.ui.init();
198205
} )
199206
.then( () => {
200-
if ( !isElement( sourceElementOrData ) && config.initialData ) {
207+
if ( !isHTMLElement && config.initialData ) {
201208
// Documented in core/editor/editorconfig.jdoc.
202209
throw new CKEditorError(
203210
'editor-create-initial-data: ' +

tests/inlineeditor.js

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
44
*/
55

6-
/* globals document, Event */
6+
/* globals document */
77

88
import InlineEditorUI from '../src/inlineeditorui';
99
import InlineEditorUIView from '../src/inlineeditoruiview';
@@ -68,37 +68,6 @@ describe( 'InlineEditor', () => {
6868
expect( editor.model.document.getRoot( 'main' ) ).to.instanceof( RootElement );
6969
} );
7070

71-
it( 'handles form element', () => {
72-
const form = document.createElement( 'form' );
73-
const textarea = document.createElement( 'textarea' );
74-
form.appendChild( textarea );
75-
document.body.appendChild( form );
76-
77-
// Prevents page realods in Firefox ;|
78-
form.addEventListener( 'submit', evt => {
79-
evt.preventDefault();
80-
} );
81-
82-
return InlineEditor.create( textarea, {
83-
plugins: [ Paragraph ]
84-
} ).then( editor => {
85-
expect( textarea.value ).to.equal( '' );
86-
87-
editor.setData( '<p>Foo</p>' );
88-
89-
form.dispatchEvent( new Event( 'submit', {
90-
// We need to be able to do preventDefault() to prevent page reloads in Firefox.
91-
cancelable: true
92-
} ) );
93-
94-
expect( textarea.value ).to.equal( '<p>Foo</p>' );
95-
96-
return editor.destroy().then( () => {
97-
form.remove();
98-
} );
99-
} );
100-
} );
101-
10271
it( 'should have undefined the #sourceElement if editor was initialized with data', () => {
10372
return InlineEditor.create( '<p>Hello world!</p>', {
10473
plugins: [ Paragraph ]
@@ -185,9 +154,19 @@ describe( 'InlineEditor', () => {
185154
InlineEditor.create( '<p>Hello world!</p>', {
186155
initialData: '<p>I am evil!</p>',
187156
plugins: [ Paragraph ]
188-
} ).catch( () => {
189-
done();
190-
} );
157+
} )
158+
.then(
159+
() => {
160+
expect.fail( 'Inline editor should throw an error when both initial data are passed' );
161+
},
162+
err => {
163+
expect( err ).to.be.an( 'error' ).with.property( 'message' ).and
164+
// eslint-disable-next-line max-len
165+
.match( /^editor-create-initial-data: The config\.initialData option cannot be used together with initial data passed in Editor\.create\(\)\./ );
166+
}
167+
)
168+
.then( done )
169+
.catch( done );
191170
} );
192171

193172
// #25
@@ -216,6 +195,21 @@ describe( 'InlineEditor', () => {
216195
return newEditor.destroy();
217196
} );
218197
} );
198+
199+
it( 'throws an error when is initialized in textarea', done => {
200+
InlineEditor.create( document.createElement( 'textarea' ) )
201+
.then(
202+
() => {
203+
expect.fail( 'Inline editor should throw an error when is initialized in textarea.' );
204+
},
205+
err => {
206+
expect( err ).to.be.an( 'error' ).with.property( 'message' ).and
207+
.match( /^editor-wrong-element: This type of editor cannot be initialized inside <textarea> element\./ );
208+
}
209+
)
210+
.then( done )
211+
.catch( done );
212+
} );
219213
} );
220214

221215
describe( 'create - events', () => {

0 commit comments

Comments
 (0)