8
0

bodycollection.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. /* global document */
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import Locale from '@ckeditor/ckeditor5-utils/src/locale';
  8. import BodyCollection from '../../src/editorui/bodycollection';
  9. import View from '../../src/view';
  10. describe( 'BodyCollection', () => {
  11. let locale;
  12. testUtils.createSinonSandbox();
  13. beforeEach( () => {
  14. locale = new Locale();
  15. } );
  16. afterEach( () => {
  17. const wrappers = Array.from( document.querySelectorAll( '.ck-body-wrapper' ) );
  18. for ( const wrapper of wrappers ) {
  19. wrapper.remove();
  20. }
  21. } );
  22. describe( 'constructor', () => {
  23. it( 'assigns locale', () => {
  24. const instance = new BodyCollection( locale );
  25. expect( instance.locale ).to.be.equal( locale );
  26. } );
  27. } );
  28. describe( 'attachToDom', () => {
  29. it( 'should create wrapper and put the collection in that wrapper', () => {
  30. const body = new BodyCollection( locale );
  31. body.attachToDom();
  32. const wrappers = Array.from( document.querySelectorAll( '.ck-body-wrapper' ) );
  33. expect( wrappers.length ).to.equal( 1 );
  34. expect( wrappers[ 0 ].parentNode ).to.equal( document.body );
  35. const el = body._bodyCollectionContainer;
  36. expect( el.parentNode ).to.equal( wrappers[ 0 ] );
  37. expect( el.classList.contains( 'ck' ) ).to.be.true;
  38. expect( el.classList.contains( 'ck-body' ) ).to.be.true;
  39. expect( el.classList.contains( 'ck-rounded-corners' ) ).to.be.true;
  40. expect( el.classList.contains( 'ck-reset_all' ) ).to.be.true;
  41. } );
  42. it( 'sets the right dir attribute to the body region (LTR)', () => {
  43. const body = new BodyCollection( locale );
  44. body.attachToDom();
  45. const el = body._bodyCollectionContainer;
  46. expect( el.getAttribute( 'dir' ) ).to.equal( 'ltr' );
  47. } );
  48. it( 'sets the right dir attribute to the body region (RTL)', () => {
  49. const locale = new Locale( { uiLanguage: 'ar' } );
  50. const body = new BodyCollection( locale );
  51. body.attachToDom();
  52. const el = body._bodyCollectionContainer;
  53. expect( el.getAttribute( 'dir' ) ).to.equal( 'rtl' );
  54. } );
  55. it( 'should put all body elements to the same wrapper', () => {
  56. const body1 = new BodyCollection( locale );
  57. body1.attachToDom();
  58. expect( document.querySelectorAll( '.ck-body-wrapper' ).length ).to.equal( 1 );
  59. expect( document.querySelectorAll( '.ck-body' ).length ).to.equal( 1 );
  60. const body2 = new BodyCollection( locale );
  61. body2.attachToDom();
  62. const bodyElements = document.querySelectorAll( '.ck-body' );
  63. expect( document.querySelectorAll( '.ck-body-wrapper' ).length ).to.equal( 1 );
  64. expect( bodyElements.length ).to.equal( 2 );
  65. expect( bodyElements[ 0 ].parentNode ).to.equal( bodyElements[ 1 ].parentNode );
  66. } );
  67. it( 'should render views in proper body collections', () => {
  68. const body1 = new BodyCollection( locale );
  69. const view1 = new View();
  70. view1.setTemplate( {
  71. tag: 'div',
  72. attributes: {
  73. class: [ 'foo' ]
  74. }
  75. } );
  76. // Should work if body is attached before the view is added...
  77. body1.attachToDom();
  78. body1.add( view1 );
  79. const body2 = new BodyCollection( locale );
  80. const view2 = new View();
  81. view2.setTemplate( {
  82. tag: 'div',
  83. attributes: {
  84. class: [ 'bar' ]
  85. }
  86. } );
  87. // ...and it should work if body is attached after the view is added.
  88. body2.add( view2 );
  89. body2.attachToDom();
  90. const wrappers = Array.from( document.querySelectorAll( '.ck-body-wrapper' ) );
  91. expect( wrappers.length ).to.equal( 1 );
  92. const wrapper = wrappers[ 0 ];
  93. const body1Element = body1._bodyCollectionContainer;
  94. const body2Element = body2._bodyCollectionContainer;
  95. expect( body1Element.parentNode ).to.equal( wrapper );
  96. expect( body1Element.childNodes.length ).to.equal( 1 );
  97. expect( body1Element.childNodes[ 0 ].classList.contains( 'foo' ) ).to.be.true;
  98. expect( body2Element.parentNode ).to.equal( wrapper );
  99. expect( body2Element.childNodes.length ).to.equal( 1 );
  100. expect( body2Element.childNodes[ 0 ].classList.contains( 'bar' ) ).to.be.true;
  101. } );
  102. } );
  103. describe( 'detachFromDom', () => {
  104. it( 'removes the body collection from DOM', () => {
  105. const body = new BodyCollection( locale );
  106. body.attachToDom();
  107. body.detachFromDom();
  108. expect( document.querySelectorAll( '.ck-body-wrapper' ).length ).to.equal( 0 );
  109. expect( document.querySelectorAll( '.ck-body' ).length ).to.equal( 0 );
  110. } );
  111. it( 'removes the multiple body collections from dom and remove the wrapper when the last is removed', () => {
  112. const body1 = new BodyCollection( locale );
  113. body1.attachToDom();
  114. const body2 = new BodyCollection( locale );
  115. body2.attachToDom();
  116. expect( document.querySelectorAll( '.ck-body-wrapper' ).length ).to.equal( 1 );
  117. expect( document.querySelectorAll( '.ck-body' ).length ).to.equal( 2 );
  118. body1.detachFromDom();
  119. expect( document.querySelectorAll( '.ck-body-wrapper' ).length ).to.equal( 1 );
  120. expect( document.querySelectorAll( '.ck-body' ).length ).to.equal( 1 );
  121. body2.detachFromDom();
  122. expect( document.querySelectorAll( '.ck-body-wrapper' ).length ).to.equal( 0 );
  123. expect( document.querySelectorAll( '.ck-body' ).length ).to.equal( 0 );
  124. } );
  125. it( 'should not throw when be called multiple times', () => {
  126. const body = new BodyCollection( locale );
  127. body.attachToDom();
  128. expect( () => {
  129. body.detachFromDom();
  130. body.detachFromDom();
  131. } ).to.not.throw();
  132. } );
  133. it( 'should not throw if attachToDom was not called before', () => {
  134. const body = new BodyCollection( locale );
  135. expect( () => {
  136. body.detachFromDom();
  137. } ).to.not.throw();
  138. } );
  139. } );
  140. } );