ckeditor.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document */
  6. import DecoupledEditor from '../src/ckeditor';
  7. import BaseDecoupledEditor from '@ckeditor/ckeditor5-editor-decoupled/src/decouplededitor';
  8. import { describeMemoryUsage, testMemoryUsage } from '@ckeditor/ckeditor5-core/tests/_utils/memory';
  9. describe( 'DecoupledEditor build', () => {
  10. let editor, editorData, editorElement;
  11. beforeEach( () => {
  12. editorData = '<p><strong>foo</strong> bar</p>';
  13. editorElement = document.createElement( 'div' );
  14. editorElement.innerHTML = editorData;
  15. document.body.appendChild( editorElement );
  16. } );
  17. afterEach( () => {
  18. editorElement.remove();
  19. editor = null;
  20. } );
  21. describe( 'build', () => {
  22. it( 'contains plugins', () => {
  23. expect( DecoupledEditor.builtinPlugins ).to.not.be.empty;
  24. } );
  25. it( 'contains config', () => {
  26. expect( DecoupledEditor.defaultConfig.toolbar ).to.not.be.empty;
  27. } );
  28. } );
  29. describe( 'editor with data', () => {
  30. test( () => editorData );
  31. it( 'does not define the UI DOM structure', () => {
  32. return DecoupledEditor.create( editorData )
  33. .then( newEditor => {
  34. expect( newEditor.ui.view.element ).to.be.null;
  35. expect( newEditor.ui.view.toolbar.element.parentElement ).to.be.null;
  36. expect( newEditor.ui.view.editable.element.parentElement ).to.be.null;
  37. return newEditor.destroy();
  38. } );
  39. } );
  40. } );
  41. describe( 'editor with editable element', () => {
  42. test( () => editorElement );
  43. it( 'uses the provided editable element', () => {
  44. return DecoupledEditor.create( editorElement )
  45. .then( newEditor => {
  46. expect( newEditor.ui.view.editable.element.parentElement ).to.equal( document.body );
  47. return newEditor.destroy();
  48. } );
  49. } );
  50. } );
  51. describeMemoryUsage( () => {
  52. testMemoryUsage(
  53. 'should not grow on multiple create/destroy',
  54. () => DecoupledEditor.create( document.querySelector( '#mem-editor' ) ) );
  55. } );
  56. function test( getEditorDataOrElement ) {
  57. describe( 'create()', () => {
  58. beforeEach( () => {
  59. return DecoupledEditor.create( getEditorDataOrElement() )
  60. .then( newEditor => {
  61. editor = newEditor;
  62. } );
  63. } );
  64. afterEach( () => {
  65. return editor.destroy();
  66. } );
  67. it( 'creates an instance which inherits from the DecoupledEditor', () => {
  68. expect( editor ).to.be.instanceof( BaseDecoupledEditor );
  69. expect( editor ).to.be.instanceof( BaseDecoupledEditor );
  70. } );
  71. it( 'loads passed data', () => {
  72. expect( editor.getData() ).to.equal( '<p><strong>foo</strong> bar</p>' );
  73. } );
  74. } );
  75. describe( 'destroy()', () => {
  76. beforeEach( () => {
  77. return DecoupledEditor.create( getEditorDataOrElement() )
  78. .then( newEditor => {
  79. editor = newEditor;
  80. } );
  81. } );
  82. } );
  83. describe( 'plugins', () => {
  84. beforeEach( () => {
  85. return DecoupledEditor.create( getEditorDataOrElement() )
  86. .then( newEditor => {
  87. editor = newEditor;
  88. } );
  89. } );
  90. afterEach( () => {
  91. return editor.destroy();
  92. } );
  93. it( 'paragraph works', () => {
  94. const data = '<p>Some text inside a paragraph.</p>';
  95. editor.setData( data );
  96. expect( editor.getData() ).to.equal( data );
  97. } );
  98. it( 'basic-styles work', () => {
  99. const data = [
  100. '<p>',
  101. '<strong>Test:strong</strong>',
  102. '<i>Test:i</i>',
  103. '<u>Test:u</u>',
  104. '<s>Test:s</s>',
  105. '</p>'
  106. ].join( '' );
  107. editor.setData( data );
  108. expect( editor.getData() ).to.equal( data );
  109. } );
  110. it( 'block-quote works', () => {
  111. const data = '<blockquote><p>Quote</p></blockquote>';
  112. editor.setData( data );
  113. expect( editor.getData() ).to.equal( data );
  114. } );
  115. it( 'heading works', () => {
  116. const data = [
  117. '<h2>Heading 1.</h2>',
  118. '<h3>Heading 1.1</h3>',
  119. '<h4>Heading 1.1.1</h4>',
  120. '<h4>Heading 1.1.2</h4>',
  121. '<h3>Heading 1.2</h3>',
  122. '<h4>Heading 1.2.1</h4>',
  123. '<h2>Heading 2</h2>'
  124. ].join( '' );
  125. editor.setData( data );
  126. expect( editor.getData() ).to.equal( data );
  127. } );
  128. it( 'image works', () => {
  129. const data = '<figure class="image"><img src="/assets/sample.png"></figure>';
  130. editor.setData( data );
  131. expect( editor.getData() ).to.equal( data );
  132. } );
  133. it( 'list works', () => {
  134. const data = [
  135. '<ul>',
  136. '<li>Item 1.</li>',
  137. '<li>Item 2.</li>',
  138. '</ul>',
  139. '<ol>',
  140. '<li>Item 1.</li>',
  141. '<li>Item 2.</li>',
  142. '</ol>'
  143. ].join( '' );
  144. editor.setData( data );
  145. expect( editor.getData() ).to.equal( data );
  146. } );
  147. it( 'list style works', () => {
  148. const data = [
  149. '<ol style="list-style-type:upper-roman;">' +
  150. '<li>Item 1.</li>' +
  151. '<li>Item 2.</li>' +
  152. '</ol>'
  153. ].join( '' );
  154. editor.setData( data );
  155. expect( editor.getData() ).to.equal( data );
  156. } );
  157. it( 'link works', () => {
  158. const data = '<p><a href="//ckeditor.com">CKEditor.com</a></p>';
  159. editor.setData( data );
  160. expect( editor.getData() ).to.equal( data );
  161. } );
  162. it( 'font size works', () => {
  163. const data = '<p><span class="text-big">foo</span></p>';
  164. editor.setData( data );
  165. expect( editor.getData() ).to.equal( data );
  166. expect( editor.model.document.selection.getAttribute( 'fontSize' ) ).to.equal( 'big' );
  167. } );
  168. it( 'font family works', () => {
  169. const data = '<p><span style="font-family:Georgia, serif;">foo</span></p>';
  170. editor.setData( data );
  171. expect( editor.getData() ).to.equal( data );
  172. expect( editor.model.document.selection.getAttribute( 'fontFamily' ) ).to.equal( 'Georgia, serif' );
  173. } );
  174. it( 'font background color works', () => {
  175. const data = '<p><span style="background-color:hsl(60,75%,60%);">foo</span></p>';
  176. editor.setData( data );
  177. expect( editor.getData() ).to.equal( data );
  178. expect( editor.model.document.selection.getAttribute( 'fontBackgroundColor' ) ).to.equal( 'hsl(60,75%,60%)' );
  179. } );
  180. it( 'font color works', () => {
  181. const data = '<p><span style="color:hsl(0,75%,60%);">foo</span></p>';
  182. editor.setData( data );
  183. expect( editor.getData() ).to.equal( data );
  184. expect( editor.model.document.selection.getAttribute( 'fontColor' ) ).to.equal( 'hsl(0,75%,60%)' );
  185. } );
  186. it( 'alignment works', () => {
  187. const data = '<p style="text-align:right;">foo</p>';
  188. editor.setData( data );
  189. expect( editor.getData() ).to.equal( data );
  190. } );
  191. } );
  192. describe( 'config', () => {
  193. afterEach( () => {
  194. return editor.destroy();
  195. } );
  196. // https://github.com/ckeditor/ckeditor5/issues/572
  197. it( 'allows configure toolbar items through config.toolbar', () => {
  198. return DecoupledEditor
  199. .create( getEditorDataOrElement(), {
  200. toolbar: [ 'bold' ]
  201. } )
  202. .then( newEditor => {
  203. editor = newEditor;
  204. expect( editor.ui.view.toolbar.items.length ).to.equal( 1 );
  205. } );
  206. } );
  207. } );
  208. }
  209. } );