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