standardeditor.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: editor, browser-only */
  6. import StandardEditor from '/ckeditor5/editor/standardeditor.js';
  7. import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/htmldataprocessor.js';
  8. import { getData, setData } from '/tests/engine/_utils/model.js';
  9. import EditingController from '/ckeditor5/engine/editingcontroller.js';
  10. import KeystrokeHandler from '/ckeditor5/keystrokehandler.js';
  11. import Feature from '/ckeditor5/feature.js';
  12. describe( 'StandardEditor', () => {
  13. let editorElement;
  14. beforeEach( () => {
  15. editorElement = document.createElement( 'div' );
  16. document.body.appendChild( editorElement );
  17. } );
  18. describe( 'constructor', () => {
  19. it( 'sets all properties', () => {
  20. const editor = new StandardEditor( editorElement, { foo: 1 } );
  21. expect( editor ).to.have.property( 'element', editorElement );
  22. expect( editor.editing ).to.be.instanceof( EditingController );
  23. expect( editor.keystrokes ).to.be.instanceof( KeystrokeHandler );
  24. } );
  25. it( 'sets config', () => {
  26. const editor = new StandardEditor( editorElement, { foo: 1 } );
  27. expect( editor.config.get( 'foo' ) ).to.equal( 1 );
  28. } );
  29. } );
  30. describe( 'create', () => {
  31. it( 'initializes editor with plugins and config', () => {
  32. class FeatureFoo extends Feature {}
  33. return StandardEditor.create( editorElement, {
  34. foo: 1,
  35. features: [ FeatureFoo ]
  36. } )
  37. .then( editor => {
  38. expect( editor ).to.be.instanceof( StandardEditor );
  39. expect( editor.config.get( 'foo' ) ).to.equal( 1 );
  40. expect( editor ).to.have.property( 'element', editorElement );
  41. expect( editor.plugins.get( FeatureFoo ) ).to.be.instanceof( FeatureFoo );
  42. } );
  43. } );
  44. } );
  45. describe( 'setData', () => {
  46. let editor;
  47. beforeEach( () => {
  48. return StandardEditor.create( editorElement )
  49. .then( newEditor => {
  50. editor = newEditor;
  51. editor.data.processor = new HtmlDataProcessor();
  52. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  53. } );
  54. } );
  55. it( 'should set data of the first root', () => {
  56. editor.document.createRoot();
  57. editor.document.createRoot( '$root', 'secondRoot' );
  58. editor.editing.createRoot( 'div' );
  59. editor.editing.createRoot( 'div', 'secondRoot' );
  60. editor.setData( 'foo' );
  61. expect( getData( editor.document, { rootName: 'main', withoutSelection: true } ) ).to.equal( 'foo' );
  62. } );
  63. } );
  64. describe( 'getData', () => {
  65. let editor;
  66. beforeEach( () => {
  67. return StandardEditor.create( editorElement )
  68. .then( newEditor => {
  69. editor = newEditor;
  70. editor.data.processor = new HtmlDataProcessor();
  71. editor.document.schema.allow( { name: '$text', inside: '$root' } );
  72. } );
  73. } );
  74. it( 'should get data of the first root', () => {
  75. editor.document.createRoot();
  76. editor.document.createRoot( '$root', 'secondRoot' );
  77. editor.editing.createRoot( 'div' );
  78. editor.editing.createRoot( 'div', 'secondRoot' );
  79. setData( editor.document, 'foo' );
  80. expect( editor.getData() ).to.equal( 'foo' );
  81. } );
  82. } );
  83. describe( 'updateEditorElement', () => {
  84. it( 'sets data to editor element', () => {
  85. const editor = new StandardEditor( editorElement );
  86. editor.data.get = () => '<p>foo</p>';
  87. editor.updateEditorElement();
  88. expect( editorElement.innerHTML ).to.equal( '<p>foo</p>' );
  89. } );
  90. } );
  91. describe( 'loadDataFromEditorElement', () => {
  92. it( 'sets data to editor element', () => {
  93. const editor = new StandardEditor( editorElement );
  94. sinon.stub( editor.data, 'set' );
  95. editorElement.innerHTML = '<p>foo</p>';
  96. editor.loadDataFromEditorElement();
  97. expect( editor.data.set.calledWithExactly( '<p>foo</p>' ) ).to.be.true;
  98. } );
  99. } );
  100. } );