8
0

editableuiview.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * @license Copyright (c) 2003-2019, 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 EditingView from '@ckeditor/ckeditor5-engine/src/view/view';
  7. import ViewRootEditableElement from '@ckeditor/ckeditor5-engine/src/view/rooteditableelement';
  8. import EditableUIView from '../../src/editableui/editableuiview';
  9. import View from '../../src/view';
  10. import Locale from '@ckeditor/ckeditor5-utils/src/locale';
  11. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. describe( 'EditableUIView', () => {
  13. let view, editableElement, editingView, editingViewRoot, locale;
  14. testUtils.createSinonSandbox();
  15. beforeEach( () => {
  16. locale = new Locale();
  17. editableElement = document.createElement( 'div' );
  18. editingView = new EditingView();
  19. editingViewRoot = new ViewRootEditableElement( 'div' );
  20. editingViewRoot._document = editingView.document;
  21. editingView.document.roots.add( editingViewRoot );
  22. view = new EditableUIView( locale, editingView );
  23. view.name = editingViewRoot.rootName;
  24. view.render();
  25. } );
  26. afterEach( () => {
  27. view.destroy();
  28. editableElement.remove();
  29. } );
  30. describe( 'constructor()', () => {
  31. it( 'sets initial values of attributes', () => {
  32. const view = new EditableUIView( locale, editingView );
  33. expect( view.isFocused ).to.be.false;
  34. expect( view.name ).to.be.null;
  35. expect( view._externalElement ).to.be.undefined;
  36. expect( view._editingView ).to.equal( editingView );
  37. view.destroy();
  38. } );
  39. it( 'renders element from template when no editableElement', () => {
  40. expect( view.element ).to.equal( view._editableElement );
  41. expect( view.element.classList.contains( 'ck' ) ).to.be.true;
  42. expect( view.element.classList.contains( 'ck-content' ) ).to.be.true;
  43. expect( view.element.classList.contains( 'ck-editor__editable' ) ).to.be.true;
  44. expect( view.element.classList.contains( 'ck-rounded-corners' ) ).to.be.true;
  45. expect( view.element.getAttribute( 'lang' ) ).to.equal( 'en' );
  46. expect( view.element.getAttribute( 'dir' ) ).to.equal( 'ltr' );
  47. expect( view._externalElement ).to.be.undefined;
  48. expect( view.isRendered ).to.be.true;
  49. } );
  50. it( 'accepts editableElement as an argument', () => {
  51. const view = new EditableUIView( locale, editingView, editableElement );
  52. view.name = editingViewRoot.rootName;
  53. view.render();
  54. expect( view.element ).to.equal( editableElement );
  55. expect( view.element ).to.equal( view._editableElement );
  56. expect( view.element.classList.contains( 'ck' ) ).to.be.true;
  57. expect( view.element.classList.contains( 'ck-editor__editable' ) ).to.be.true;
  58. expect( view.element.classList.contains( 'ck-rounded-corners' ) ).to.be.true;
  59. expect( view.element.getAttribute( 'lang' ) ).to.equal( 'en' );
  60. expect( view.element.getAttribute( 'dir' ) ).to.equal( 'ltr' );
  61. expect( view._hasExternalElement ).to.be.true;
  62. expect( view.isRendered ).to.be.true;
  63. view.destroy();
  64. } );
  65. it( 'sets proper lang and dir attributes (implicit content language)', () => {
  66. const locale = new Locale( { uiLanguage: 'ar' } );
  67. const view = new EditableUIView( locale, editingView );
  68. view.name = editingViewRoot.rootName;
  69. view.render();
  70. expect( view.element.getAttribute( 'lang' ) ).to.equal( 'ar' );
  71. expect( view.element.getAttribute( 'dir' ) ).to.equal( 'rtl' );
  72. view.destroy();
  73. } );
  74. it( 'sets proper lang and dir attributes (explicit content language)', () => {
  75. const locale = new Locale( {
  76. uiLanguage: 'pl',
  77. contentLanguage: 'ar'
  78. } );
  79. const view = new EditableUIView( locale, editingView );
  80. view.name = editingViewRoot.rootName;
  81. view.render();
  82. expect( view.element.getAttribute( 'lang' ) ).to.equal( 'ar' );
  83. expect( view.element.getAttribute( 'dir' ) ).to.equal( 'rtl' );
  84. view.destroy();
  85. } );
  86. } );
  87. describe( 'View bindings', () => {
  88. describe( 'class', () => {
  89. it( 'reacts on view#isFocused', () => {
  90. view.isFocused = true;
  91. expect( editingViewRoot.hasClass( 'ck-focused' ) ).to.be.true;
  92. expect( editingViewRoot.hasClass( 'ck-blurred' ) ).to.be.false;
  93. view.isFocused = false;
  94. expect( editingViewRoot.hasClass( 'ck-focused' ) ).to.be.false;
  95. expect( editingViewRoot.hasClass( 'ck-blurred' ) ).to.be.true;
  96. } );
  97. // https://github.com/ckeditor/ckeditor5/issues/1530.
  98. // https://github.com/ckeditor/ckeditor5/issues/1676.
  99. it( 'should work when update is handled during the rendering phase', () => {
  100. const secondEditingViewRoot = new ViewRootEditableElement( 'div' );
  101. const secondView = new EditableUIView( locale, editingView );
  102. const secondEditableElement = document.createElement( 'div' );
  103. document.body.appendChild( secondEditableElement );
  104. secondEditingViewRoot.rootName = 'second';
  105. secondEditingViewRoot._document = editingView.document;
  106. editingView.document.roots.add( secondEditingViewRoot );
  107. secondView.name = 'second';
  108. secondView.render();
  109. editingView.attachDomRoot( editableElement, 'main' );
  110. editingView.attachDomRoot( secondEditableElement, 'second' );
  111. view.isFocused = true;
  112. secondView.isFocused = false;
  113. expect( editingViewRoot.hasClass( 'ck-focused' ), 1 ).to.be.true;
  114. expect( editingViewRoot.hasClass( 'ck-blurred' ), 2 ).to.be.false;
  115. expect( secondEditingViewRoot.hasClass( 'ck-focused' ), 3 ).to.be.false;
  116. expect( secondEditingViewRoot.hasClass( 'ck-blurred' ), 4 ).to.be.true;
  117. editingView.isRenderingInProgress = true;
  118. view.isFocused = false;
  119. secondView.isFocused = true;
  120. expect( editingViewRoot.hasClass( 'ck-focused' ), 5 ).to.be.true;
  121. expect( editingViewRoot.hasClass( 'ck-blurred' ), 6 ).to.be.false;
  122. expect( secondEditingViewRoot.hasClass( 'ck-focused' ), 7 ).to.be.false;
  123. expect( secondEditingViewRoot.hasClass( 'ck-blurred' ), 8 ).to.be.true;
  124. editingView.isRenderingInProgress = false;
  125. expect( editingViewRoot.hasClass( 'ck-focused' ), 9 ).to.be.false;
  126. expect( editingViewRoot.hasClass( 'ck-blurred' ), 10 ).to.be.true;
  127. expect( secondEditingViewRoot.hasClass( 'ck-focused' ), 11 ).to.be.true;
  128. expect( secondEditingViewRoot.hasClass( 'ck-blurred' ), 12 ).to.be.false;
  129. secondEditableElement.remove();
  130. secondView.destroy();
  131. } );
  132. } );
  133. } );
  134. describe( 'destroy()', () => {
  135. it( 'calls super#destroy()', () => {
  136. const spy = testUtils.sinon.spy( View.prototype, 'destroy' );
  137. const view = new EditableUIView( locale, editingView );
  138. view.name = editingViewRoot.rootName;
  139. view.render();
  140. view.destroy();
  141. sinon.assert.calledOnce( spy );
  142. } );
  143. it( 'can be called multiple times', () => {
  144. const view = new EditableUIView( locale, editingView );
  145. view.name = editingViewRoot.rootName;
  146. view.render();
  147. expect( () => {
  148. view.destroy();
  149. view.destroy();
  150. } ).to.not.throw();
  151. } );
  152. describe( 'when #editableElement as an argument', () => {
  153. it( 'reverts the template of editableElement', () => {
  154. const editableElement = document.createElement( 'div' );
  155. editableElement.classList.add( 'foo' );
  156. editableElement.contentEditable = false;
  157. const view = new EditableUIView( locale, editingView, editableElement );
  158. view.name = editingViewRoot.rootName;
  159. view.render();
  160. view.destroy();
  161. expect( view.element.classList.contains( 'ck' ) ).to.be.false;
  162. expect( view.element.classList.contains( 'foo' ) ).to.be.true;
  163. } );
  164. } );
  165. } );
  166. } );