@@ -10,11 +10,13 @@ import View from '@ckeditor/ckeditor5-ui/src/view';
1010import InlineEditorUI from '../src/inlineeditorui' ;
1111import EditorUI from '@ckeditor/ckeditor5-core/src/editor/editorui' ;
1212import InlineEditorUIView from '../src/inlineeditoruiview' ;
13+ import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph' ;
1314import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor' ;
1415
1516import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard' ;
1617import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils' ;
1718import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils' ;
19+ import { isElement } from 'lodash-es' ;
1820
1921describe ( 'InlineEditorUI' , ( ) => {
2022 let editor , view , ui , viewElement ;
@@ -23,7 +25,7 @@ describe( 'InlineEditorUI', () => {
2325
2426 beforeEach ( ( ) => {
2527 return VirtualInlineTestEditor
26- . create ( {
28+ . create ( 'foo' , {
2729 toolbar : [ 'foo' , 'bar' ] ,
2830 } )
2931 . then ( newEditor => {
@@ -65,7 +67,7 @@ describe( 'InlineEditorUI', () => {
6567
6668 it ( 'sets view#viewportTopOffset, if specified' , ( ) => {
6769 return VirtualInlineTestEditor
68- . create ( {
70+ . create ( 'foo' , {
6971 toolbar : {
7072 viewportTopOffset : 100
7173 }
@@ -128,23 +130,66 @@ describe( 'InlineEditorUI', () => {
128130 { isFocused : true }
129131 ) ;
130132 } ) ;
133+ } ) ;
131134
132- it ( 'binds view.editable#isReadOnly' , ( ) => {
133- utils . assertBinding (
134- view . editable ,
135- { isReadOnly : false } ,
136- [
137- [ editable , { isReadOnly : true } ]
138- ] ,
139- { isReadOnly : true }
140- ) ;
135+ describe ( 'placeholder' , ( ) => {
136+ it ( 'sets placeholder from editor.config.placeholder' , ( ) => {
137+ return VirtualInlineTestEditor
138+ . create ( 'foo' , {
139+ extraPlugins : [ Paragraph ] ,
140+ placeholder : 'placeholder-text' ,
141+ } )
142+ . then ( newEditor => {
143+ const firstChild = newEditor . editing . view . document . getRoot ( ) . getChild ( 0 ) ;
144+
145+ expect ( firstChild . getAttribute ( 'data-placeholder' ) ) . to . equal ( 'placeholder-text' ) ;
146+
147+ return newEditor . destroy ( ) ;
148+ } ) ;
149+ } ) ;
150+
151+ it ( 'sets placeholder from "placeholder" attribute of a passed element' , ( ) => {
152+ const element = document . createElement ( 'div' ) ;
153+
154+ element . setAttribute ( 'placeholder' , 'placeholder-text' ) ;
155+
156+ return VirtualInlineTestEditor
157+ . create ( element , {
158+ extraPlugins : [ Paragraph ]
159+ } )
160+ . then ( newEditor => {
161+ const firstChild = newEditor . editing . view . document . getRoot ( ) . getChild ( 0 ) ;
162+
163+ expect ( firstChild . getAttribute ( 'data-placeholder' ) ) . to . equal ( 'placeholder-text' ) ;
164+
165+ return newEditor . destroy ( ) ;
166+ } ) ;
167+ } ) ;
168+
169+ it ( 'uses editor.config.placeholder rather than "placeholder" attribute of a passed element' , ( ) => {
170+ const element = document . createElement ( 'div' ) ;
171+
172+ element . setAttribute ( 'placeholder' , 'placeholder-text' ) ;
173+
174+ return VirtualInlineTestEditor
175+ . create ( element , {
176+ placeholder : 'config takes precedence' ,
177+ extraPlugins : [ Paragraph ]
178+ } )
179+ . then ( newEditor => {
180+ const firstChild = newEditor . editing . view . document . getRoot ( ) . getChild ( 0 ) ;
181+
182+ expect ( firstChild . getAttribute ( 'data-placeholder' ) ) . to . equal ( 'config takes precedence' ) ;
183+
184+ return newEditor . destroy ( ) ;
185+ } ) ;
141186 } ) ;
142187 } ) ;
143188
144189 describe ( 'view.toolbar#items' , ( ) => {
145190 it ( 'are filled with the config.toolbar (specified as an Array)' , ( ) => {
146191 return VirtualInlineTestEditor
147- . create ( {
192+ . create ( '' , {
148193 toolbar : [ 'foo' , 'bar' ]
149194 } )
150195 . then ( editor => {
@@ -159,7 +204,7 @@ describe( 'InlineEditorUI', () => {
159204
160205 it ( 'are filled with the config.toolbar (specified as an Object)' , ( ) => {
161206 return VirtualInlineTestEditor
162- . create ( {
207+ . create ( '' , {
163208 toolbar : {
164209 items : [ 'foo' , 'bar' ] ,
165210 viewportTopOffset : 100
@@ -177,7 +222,7 @@ describe( 'InlineEditorUI', () => {
177222 } ) ;
178223
179224 it ( 'initializes keyboard navigation between view#toolbar and view#editable' , ( ) => {
180- return VirtualInlineTestEditor . create ( )
225+ return VirtualInlineTestEditor . create ( '' )
181226 . then ( editor => {
182227 const ui = editor . ui ;
183228 const view = ui . view ;
@@ -200,6 +245,47 @@ describe( 'InlineEditorUI', () => {
200245 } ) ;
201246 } ) ;
202247
248+ describe ( 'destroy()' , ( ) => {
249+ it ( 'detaches the DOM root then destroys the UI view' , ( ) => {
250+ return VirtualInlineTestEditor . create ( '' )
251+ . then ( newEditor => {
252+ const destroySpy = sinon . spy ( newEditor . ui . view , 'destroy' ) ;
253+ const detachSpy = sinon . spy ( newEditor . editing . view , 'detachDomRoot' ) ;
254+
255+ return newEditor . destroy ( )
256+ . then ( ( ) => {
257+ sinon . assert . callOrder ( detachSpy , destroySpy ) ;
258+ } ) ;
259+ } ) ;
260+ } ) ;
261+
262+ it ( 'restores the editor element back to its original state' , ( ) => {
263+ const domElement = document . createElement ( 'div' ) ;
264+
265+ domElement . setAttribute ( 'foo' , 'bar' ) ;
266+ domElement . setAttribute ( 'data-baz' , 'qux' ) ;
267+ domElement . classList . add ( 'foo-class' ) ;
268+
269+ return VirtualInlineTestEditor . create ( domElement )
270+ . then ( newEditor => {
271+ return newEditor . destroy ( )
272+ . then ( ( ) => {
273+ const attributes = { } ;
274+
275+ for ( const attribute of domElement . attributes ) {
276+ attributes [ attribute . name ] = attribute . value ;
277+ }
278+
279+ expect ( attributes ) . to . deep . equal ( {
280+ foo : 'bar' ,
281+ 'data-baz' : 'qux' ,
282+ class : 'foo-class'
283+ } ) ;
284+ } ) ;
285+ } ) ;
286+ } ) ;
287+ } ) ;
288+
203289 describe ( 'element()' , ( ) => {
204290 it ( 'returns correct element instance' , ( ) => {
205291 expect ( ui . element ) . to . equal ( viewElement ) ;
@@ -233,10 +319,14 @@ function viewCreator( name ) {
233319}
234320
235321class VirtualInlineTestEditor extends VirtualTestEditor {
236- constructor ( config ) {
322+ constructor ( sourceElementOrData , config ) {
237323 super ( config ) ;
238324
239- const view = new InlineEditorUIView ( this . locale ) ;
325+ if ( isElement ( sourceElementOrData ) ) {
326+ this . sourceElement = sourceElementOrData ;
327+ }
328+
329+ const view = new InlineEditorUIView ( this . locale , this . editing . view ) ;
240330 this . ui = new InlineEditorUI ( this , view ) ;
241331
242332 this . ui . componentFactory . add ( 'foo' , viewCreator ( 'foo' ) ) ;
@@ -249,14 +339,20 @@ class VirtualInlineTestEditor extends VirtualTestEditor {
249339 return super . destroy ( ) ;
250340 }
251341
252- static create ( config ) {
342+ static create ( sourceElementOrData , config ) {
253343 return new Promise ( resolve => {
254- const editor = new this ( config ) ;
344+ const editor = new this ( sourceElementOrData , config ) ;
255345
256346 resolve (
257347 editor . initPlugins ( )
258348 . then ( ( ) => {
259349 editor . ui . init ( ) ;
350+
351+ const initialData = isElement ( sourceElementOrData ) ?
352+ sourceElementOrData . innerHTML :
353+ sourceElementOrData ;
354+
355+ editor . data . init ( initialData ) ;
260356 editor . fire ( 'ready' ) ;
261357 } )
262358 . then ( ( ) => editor )
0 commit comments