editableuiview.js 7.3 KB

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