]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Preserve object prototypes when cloning (#7381)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Wed, 20 May 2020 21:40:42 +0000 (00:40 +0300)
committerGitHub <noreply@github.com>
Wed, 20 May 2020 21:40:42 +0000 (17:40 -0400)
src/helpers/helpers.core.js
test/specs/helpers.core.tests.js

index ffcc0eae38a7e5632c8a021fdfa8cede7deb0f9b..0e5ad2a4b4c72bfe50970097ad221efc90e23249 100644 (file)
@@ -199,7 +199,7 @@ export function clone(source) {
        }
 
        if (isObject(source)) {
-               const target = {};
+               const target = Object.create(source);
                const keys = Object.keys(source);
                const klen = keys.length;
                let k = 0;
index 869e9dfd182597ba11f0e6a13385e9aa00e7aa86..2586173e78916af210bcffef70bc0603497d80bd 100644 (file)
@@ -360,6 +360,27 @@ describe('Chart.helpers.core', function() {
                        expect(output.o).not.toBe(o0);
                        expect(output.o.a).not.toBe(a1);
                });
+               it('should preserve prototype of objects', function() {
+                       // https://github.com/chartjs/Chart.js/issues/7340
+                       class MyConfigObject {
+                               constructor(s) {
+                                       this._s = s;
+                               }
+                               func() {
+                                       return 10;
+                               }
+                       }
+                       var original = new MyConfigObject('something');
+                       var output = helpers.merge({}, {
+                               plugins: [{
+                                       test: original
+                               }]
+                       });
+                       var clone = output.plugins[0].test;
+                       expect(clone).toBeInstanceOf(MyConfigObject);
+                       expect(clone).toEqual(original);
+                       expect(clone === original).toBeFalse();
+               });
        });
 
        describe('mergeIf', function() {