]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Preserve object prototypes when cloning (#7404)
authorBryan Iddings <bryan.iddings@gmail.com>
Thu, 4 Jun 2020 21:23:59 +0000 (17:23 -0400)
committerGitHub <noreply@github.com>
Thu, 4 Jun 2020 21:23:59 +0000 (00:23 +0300)
Co-authored-by: Bryan.Iddings <bryan.iddings@noaa.gov>
src/helpers/helpers.core.js
test/specs/helpers.core.tests.js

index 350a207509124a4aa64747c8fad72ac6b48dea8a..6cf28c00d416beff59f508b3ed9f37932da4a35f 100644 (file)
@@ -175,7 +175,7 @@ var helpers = {
                }
 
                if (helpers.isObject(source)) {
-                       var target = {};
+                       var target = Object.create(source);
                        var keys = Object.keys(source);
                        var klen = keys.length;
                        var k = 0;
index cdda01b967413cc1423451a2a1d5d60661ab9e50..1f524089a216fd79fdeb552311a2d36004d32074 100644 (file)
@@ -301,6 +301,25 @@ describe('Chart.helpers.core', function() {
                        expect(output.o.a).not.toBe(a1);
                        expect(output.a).not.toBe(a0);
                });
+               it('should preserve prototype of objects', function() {
+                       // https://github.com/chartjs/Chart.js/issues/7340
+                       function MyConfigObject(s) {
+                               this._s = s;
+                       }
+                       MyConfigObject.prototype.func = function() {
+                               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('merge', function() {