Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toObjectMethod being called by different argument types #5549

Closed
anthify opened this issue Mar 3, 2019 · 10 comments
Closed

toObjectMethod being called by different argument types #5549

anthify opened this issue Mar 3, 2019 · 10 comments

Comments

@anthify
Copy link
Contributor

anthify commented Mar 3, 2019

Version

EDIT: 2.4.0 2.6.0

Details

Firstly, sorry to go off the issue format. I'm working on a biggish project which extends quite a few different classes so it might be a problem of my own making.

The _toObjectMethod is called recursively if clipPath resolves to true, but instead of methodName being passed which (afaik) should be a string, and in this instance should have the value"toObject". But as you can see below it is called with whatever is assigned to clipPath, which in my case is a fabric.js klass, and this breaks the following call to this._toObjects>this._toObject when trying to locate a methodName on the klass, but the methodName is also a klass assigned to clipPath.

    /**
     * @private
     */
    _toObjectMethod: function (methodName, propertiesToInclude) {

      var clipPath = this.clipPath, data = {
        version: fabric.version,
        objects: this._toObjects(methodName, propertiesToInclude),
      };
      if (clipPath) {
        /** the line below passes clipPath into should be methodName **/
        data.clipPath = this._toObjectMethod(clipPath, methodName, propertiesToInclude);
      }
      extend(data, this.__serializeBgOverlay(methodName, propertiesToInclude));

      fabric.util.populateWithProperties(this, data, propertiesToInclude);

      return data;
    },
    /**
     * @private
     */
    _toObjects: function(methodName, propertiesToInclude) {
      return this._objects.filter(function(object) {
        return !object.excludeFromExport;
      }).map(function(instance) {
        return this._toObject(instance, methodName, propertiesToInclude);
      }, this);
    },

    /**
     * @private
     */
    _toObject: function(instance, methodName, propertiesToInclude) {
      var originalValue;

      if (!this.includeDefaultValues) {
        originalValue = instance.includeDefaultValues;
        instance.includeDefaultValues = false;
      }
     
     /** it breaks here as methodName is in fact the clipPath klass **/
      var object = instance[methodName](propertiesToInclude);
      if (!this.includeDefaultValues) {
        instance.includeDefaultValues = originalValue;
      }
      return object;
    },

For my use I've done following, which seems to work 🤞

fabric.Canvas.prototype._toObjectMethod = function (methodName, propertiesToInclude) {
  var clipPath = this.clipPath, data = {
    version: fabric.version,
    objects: this._toObjects(methodName, propertiesToInclude),
  };
  if (clipPath) {
    data.clipPath = clipPath;
  }
  fabric.util.object.extend(data, this.__serializeBgOverlay(methodName, propertiesToInclude));

  fabric.util.populateWithProperties(this, data, propertiesToInclude);

  return data;
};

I'm probably misunderstanding the code here, so any clarification would be useful.

Thanks

@anthify anthify changed the title toObjecMethod being called by different argument types toObjectMethod being called by different argument types Mar 3, 2019
@asturur
Copy link
Member

asturur commented Mar 3, 2019

well i do not know why this should be so broken and things works.
That should be clipPath.toObjectMethod() maybe a typo?
That should be either _toObject ... or i do not know.
I would expect UTs to catch it.

@asturur
Copy link
Member

asturur commented Mar 3, 2019

Definetely a bug....
that should be _toObject.

Do you mind opening a PR with a test that fails and then add the fix and make test passing?

@asturur asturur added the bug label Mar 3, 2019
@asturur
Copy link
Member

asturur commented Mar 3, 2019

You should still try to reproduce the bug. I bet that if you add a canvas with a clipPath and then you do toObject, it will fail. won't it?

@asturur
Copy link
Member

asturur commented Mar 3, 2019

http://jsfiddle.net/52sp0hev/
^^^ this will make error indeed.

@anthify
Copy link
Contributor Author

anthify commented Mar 3, 2019

@asturur, sure will open a PR

@anthify
Copy link
Contributor Author

anthify commented Mar 3, 2019

@asturur should I branch from master? A lot of unit tests are already failing. Is there a recommended node version I should be using with this repo?

@asturur
Copy link
Member

asturur commented Mar 3, 2019

any node between 4 and 6.
Tests do not fail if you install canvas properly and build before testing.

npm install
npm run build
npm run test

@anthify
Copy link
Contributor Author

anthify commented Mar 3, 2019

Cool, will sort it out tomorrow.

@anthify
Copy link
Contributor Author

anthify commented Mar 5, 2019

@asturur PR Raised #5556

@asturur
Copy link
Member

asturur commented Mar 23, 2019

this is fixed now

@asturur asturur closed this as completed Mar 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants