This repository was archived by the owner on Jun 26, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +43
-3
lines changed
Expand file tree Collapse file tree 2 files changed +43
-3
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 33 * For licensing, see LICENSE.md.
44 */
55
6+ /* global document */
7+
68import Config from '../src/config' ;
79
810describe ( '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} ) ;
You can’t perform that action at this time.
0 commit comments