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

Commit

Permalink
Merge pull request #265 from ckeditor/t/264
Browse files Browse the repository at this point in the history
Other: DOM Elements will not be cloned when returned from config.get. Closes #264.
  • Loading branch information
oskarwrobel authored Jan 3, 2019
2 parents 7e6bd66 + 9f180ff commit 4ad23b1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @module utils/config
*/

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

/**
* Handles a configuration dictionary.
Expand Down Expand Up @@ -197,8 +197,8 @@ export default class Config {
source = source[ part ];
}

// Always returns undefined for non existing configuration
return source ? cloneDeep( source[ name ] ) : undefined;
// Always returns undefined for non existing configuration.
return source ? cloneConfig( source[ name ] ) : undefined;
}

/**
Expand All @@ -215,3 +215,19 @@ export default class Config {
} );
}
}

// Clones configuration object or value.
// @param {*} source Source configuration
// @returns {*} Cloned configuration value.
function cloneConfig( source ) {
return cloneDeepWith( source, leaveDOMReferences );
}

// A customizer function for cloneDeepWith.
// It will leave references to DOM Elements instead of cloning them.
//
// @param {*} value
// @returns {Element|undefined}
function leaveDOMReferences( value ) {
return isElement( value ) ? value : undefined;
}
24 changes: 24 additions & 0 deletions tests/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* For licensing, see LICENSE.md.
*/

/* global document */

import Config from '../src/config';

describe( 'Config', () => {
Expand Down Expand Up @@ -434,5 +436,27 @@ describe( 'Config', () => {
// But array members should remain the same contents should be equal:
expect( pluginsAgain ).to.deep.equal( plugins );
} );

it( 'should return DOM nodes references from config array', () => {
const foo = document.createElement( 'div' );

config.set( 'node', foo );
config.set( 'nodes', [ foo ] );

expect( config.get( 'node' ) ).to.equal( foo );
expect( config.get( 'nodes' ) ).to.deep.equal( [ foo ] );

const nodes = config.get( 'nodes' );

expect( nodes[ 0 ] ).to.equal( foo );

const nodesAgain = config.get( 'nodes' );

// The returned array should be a new instance:
expect( nodesAgain ).to.not.equal( nodes );

// But array members should remain the same contents should be equal:
expect( nodesAgain ).to.deep.equal( nodes );
} );
} );
} );

0 comments on commit 4ad23b1

Please sign in to comment.