Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit 4ad23b1

Browse files
authored
Merge pull request #265 from ckeditor/t/264
Other: DOM Elements will not be cloned when returned from config.get. Closes #264.
2 parents 7e6bd66 + 9f180ff commit 4ad23b1

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/config.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @module utils/config
88
*/
99

10-
import { isPlainObject, cloneDeep } from 'lodash-es';
10+
import { isPlainObject, isElement, cloneDeepWith } from 'lodash-es';
1111

1212
/**
1313
* Handles a configuration dictionary.
@@ -197,8 +197,8 @@ export default class Config {
197197
source = source[ part ];
198198
}
199199

200-
// Always returns undefined for non existing configuration
201-
return source ? cloneDeep( source[ name ] ) : undefined;
200+
// Always returns undefined for non existing configuration.
201+
return source ? cloneConfig( source[ name ] ) : undefined;
202202
}
203203

204204
/**
@@ -215,3 +215,19 @@ export default class Config {
215215
} );
216216
}
217217
}
218+
219+
// Clones configuration object or value.
220+
// @param {*} source Source configuration
221+
// @returns {*} Cloned configuration value.
222+
function cloneConfig( source ) {
223+
return cloneDeepWith( source, leaveDOMReferences );
224+
}
225+
226+
// A customizer function for cloneDeepWith.
227+
// It will leave references to DOM Elements instead of cloning them.
228+
//
229+
// @param {*} value
230+
// @returns {Element|undefined}
231+
function leaveDOMReferences( value ) {
232+
return isElement( value ) ? value : undefined;
233+
}

tests/config.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* For licensing, see LICENSE.md.
44
*/
55

6+
/* global document */
7+
68
import Config from '../src/config';
79

810
describe( 'Config', () => {
@@ -434,5 +436,27 @@ describe( 'Config', () => {
434436
// But array members should remain the same contents should be equal:
435437
expect( pluginsAgain ).to.deep.equal( plugins );
436438
} );
439+
440+
it( 'should return DOM nodes references from config array', () => {
441+
const foo = document.createElement( 'div' );
442+
443+
config.set( 'node', foo );
444+
config.set( 'nodes', [ foo ] );
445+
446+
expect( config.get( 'node' ) ).to.equal( foo );
447+
expect( config.get( 'nodes' ) ).to.deep.equal( [ foo ] );
448+
449+
const nodes = config.get( 'nodes' );
450+
451+
expect( nodes[ 0 ] ).to.equal( foo );
452+
453+
const nodesAgain = config.get( 'nodes' );
454+
455+
// The returned array should be a new instance:
456+
expect( nodesAgain ).to.not.equal( nodes );
457+
458+
// But array members should remain the same contents should be equal:
459+
expect( nodesAgain ).to.deep.equal( nodes );
460+
} );
437461
} );
438462
} );

0 commit comments

Comments
 (0)