Skip to content

Commit

Permalink
Merge pull request #17465 from ckeditor/ck/optimize-body-wrapper-look…
Browse files Browse the repository at this point in the history
…up-followup

Internal (ui): `BodyCollection#attachToDom()` should create another wrapper if the previous one got disconnected from DOM.
  • Loading branch information
oleq authored Nov 14, 2024
2 parents 298c449 + 28a9f1e commit 09b6dcc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/ckeditor5-ui/src/editorui/bodycollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ export default class BodyCollection extends ViewCollection {
children: this
} ).render() as HTMLElement;

if ( !BodyCollection._bodyWrapper ) {
// Create a shared wrapper if there were none or the previous one got disconnected from DOM.
if ( !BodyCollection._bodyWrapper || !BodyCollection._bodyWrapper.isConnected ) {
BodyCollection._bodyWrapper = createElement( document, 'div', { class: 'ck-body-wrapper' } );
document.body.appendChild( BodyCollection._bodyWrapper );
}
Expand Down
45 changes: 45 additions & 0 deletions packages/ckeditor5-ui/tests/editorui/bodycollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,51 @@ describe( 'BodyCollection', () => {
expect( BodyCollection._bodyWrapper ).to.equal( bodyElements[ 0 ].parentNode );
} );

it( 'should create another wrapper if the previous one got disconnected from DOM', () => {
const body1 = new BodyCollection( locale );
body1.attachToDom();

let wrappers, bodyContainers;

wrappers = document.querySelectorAll( '.ck-body-wrapper' );
bodyContainers = document.querySelectorAll( '.ck-body' );

expect( wrappers.length ).to.equal( 1 );
expect( bodyContainers.length ).to.equal( 1 );

// Some external code breaks the wrapper.
wrappers[ 0 ].remove();

const body2 = new BodyCollection( locale );
body2.attachToDom();

wrappers = document.querySelectorAll( '.ck-body-wrapper' );
bodyContainers = document.querySelectorAll( '.ck-body' );

expect( wrappers.length ).to.equal( 1 );
expect( bodyContainers.length ).to.equal( 1 );
expect( bodyContainers[ 0 ] ).to.equal( body2.bodyCollectionContainer );
expect( body2.bodyCollectionContainer.parentElement ).to.equal( wrappers[ 0 ] );

body1.detachFromDom();

wrappers = document.querySelectorAll( '.ck-body-wrapper' );
bodyContainers = document.querySelectorAll( '.ck-body' );

expect( wrappers.length ).to.equal( 1 );
expect( bodyContainers.length ).to.equal( 1 );
expect( bodyContainers[ 0 ] ).to.equal( body2.bodyCollectionContainer );
expect( body2.bodyCollectionContainer.parentElement ).to.equal( wrappers[ 0 ] );

body2.detachFromDom();

wrappers = document.querySelectorAll( '.ck-body-wrapper' );
bodyContainers = document.querySelectorAll( '.ck-body' );

expect( wrappers.length ).to.equal( 0 );
expect( bodyContainers.length ).to.equal( 0 );
} );

it( 'should render views in proper body collections', () => {
const body1 = new BodyCollection( locale );

Expand Down

0 comments on commit 09b6dcc

Please sign in to comment.