]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Pass object from array as value to _fallback (#9969)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Wed, 8 Dec 2021 14:27:55 +0000 (16:27 +0200)
committerGitHub <noreply@github.com>
Wed, 8 Dec 2021 14:27:55 +0000 (16:27 +0200)
* Pass object from array as value to _fallback

* cleanup

* reduce changes

* reduce even more changes

src/helpers/helpers.config.js
test/specs/helpers.config.tests.js

index 30567c912ec9f47ef6671c91e64a5a33a6541676..ef86bfaa5b738dcaa21b1f8af393c76580829532 100644 (file)
@@ -250,12 +250,12 @@ function resolveFallback(fallback, prop, value) {
 const getScope = (key, parent) => key === true ? parent
   : typeof key === 'string' ? resolveObjectKey(parent, key) : undefined;
 
-function addScopes(set, parentScopes, key, parentFallback) {
+function addScopes(set, parentScopes, key, parentFallback, value) {
   for (const parent of parentScopes) {
     const scope = getScope(key, parent);
     if (scope) {
       set.add(scope);
-      const fallback = resolveFallback(scope._fallback, key, scope);
+      const fallback = resolveFallback(scope._fallback, key, value);
       if (defined(fallback) && fallback !== key && fallback !== parentFallback) {
         // When we reach the descriptor that defines a new _fallback, return that.
         // The fallback will resume to that new scope.
@@ -276,12 +276,12 @@ function createSubResolver(parentScopes, resolver, prop, value) {
   const allScopes = [...parentScopes, ...rootScopes];
   const set = new Set();
   set.add(value);
-  let key = addScopesFromKey(set, allScopes, prop, fallback || prop);
+  let key = addScopesFromKey(set, allScopes, prop, fallback || prop, value);
   if (key === null) {
     return false;
   }
   if (defined(fallback) && fallback !== prop) {
-    key = addScopesFromKey(set, allScopes, fallback, key);
+    key = addScopesFromKey(set, allScopes, fallback, key, value);
     if (key === null) {
       return false;
     }
@@ -290,9 +290,9 @@ function createSubResolver(parentScopes, resolver, prop, value) {
     () => subGetTarget(resolver, prop, value));
 }
 
-function addScopesFromKey(set, allScopes, key, fallback) {
+function addScopesFromKey(set, allScopes, key, fallback, item) {
   while (key) {
-    key = addScopes(set, allScopes, key, fallback);
+    key = addScopes(set, allScopes, key, fallback, item);
   }
   return key;
 }
index 76d056a33279644bef90af9492985e8f0af12948..f78782216ef1e2462a46b40c9d27333c31d744cd 100644 (file)
@@ -654,6 +654,46 @@ describe('Chart.helpers.config', function() {
       });
     });
 
+    it('should call _fallback with proper value from array when descriptor is object', function() {
+      const spy = jasmine.createSpy('fallback');
+      const descriptors = {
+        items: {
+          _fallback: spy
+        }
+      };
+      const options = {
+        items: [{test: true}]
+      };
+      const resolver = _createResolver([options, descriptors]);
+      const opts = _attachContext(resolver, {dymmy: true});
+      const item0 = opts.items[0];
+      expect(item0.test).toEqual(true);
+      expect(spy).toHaveBeenCalledWith('items', options.items[0]);
+    });
+
+    it('should call _fallback with proper value from array when descriptor and defaults are objects', function() {
+      const spy = jasmine.createSpy('fallback');
+      const descriptors = {
+        items: {
+          _fallback: spy
+        }
+      };
+      const defaults = {
+        items: {
+          type: 'defaultType'
+        }
+      };
+      const options = {
+        items: [{test: true}]
+      };
+      const resolver = _createResolver([options, defaults, descriptors]);
+      const opts = _attachContext(resolver, {dymmy: true});
+      const item0 = opts.items[0];
+      console.warn(opts._proxy._scopes);
+      expect(item0.test).toEqual(true);
+      expect(spy).toHaveBeenCalledWith('items', options.items[0]);
+    });
+
     it('should support overriding options', function() {
       const options = {
         fn1: ctx => ctx.index,