bodycollection.js 5.5 KB

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