Kaynağa Gözat

Leave DOM Elements in configuration object returned from editor.config.get().

Maciej Gołaszewski 7 yıl önce
ebeveyn
işleme
66abe08049

+ 19 - 3
packages/ckeditor5-utils/src/config.js

@@ -7,7 +7,7 @@
  * @module utils/config
  */
 
-import { isPlainObject, cloneDeep } from 'lodash-es';
+import { isPlainObject, isElement, cloneDeepWith } from 'lodash-es';
 
 /**
  * Handles a configuration dictionary.
@@ -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;
 	}
 
 	/**
@@ -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 - 0
packages/ckeditor5-utils/tests/config.js

@@ -3,6 +3,8 @@
  * For licensing, see LICENSE.md.
  */
 
+/* global document */
+
 import Config from '../src/config';
 
 describe( 'Config', () => {
@@ -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 );
+		} );
 	} );
 } );