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

feat(Vectorizer): add option to support camel case attributes #2339

Merged

Conversation

kumilingus
Copy link
Contributor

PR allows to use of both kebab-case and camel-case attribute names when setting/reading/removing attributes to/from SVGNodes.

vel.attr('strokeWidth', 2);
vel.attr({ strokeOpacity: 0.5, stroke: 'red' });
vel.removeAttr('strokeWidth');

The feature needs to be enabled with V.supportCamelCaseAttributes = true;.

@alexandernst
Copy link
Contributor

Why not enabled it by default? 🤔 Maybe perf concerns?

@kumilingus
Copy link
Contributor Author

Would not that be a breaking change? e.g. if someone used the camel case attributes on purpose.

vel.attr('myAttribute', 5); 
vel.node.getAttribute('myAttribute')  // => null

The plan is to release it as an experimental feature as part of the upcoming patch release.

@alexandernst
Copy link
Contributor

But that change will be merged into v4, which will introduce breaking changes anyways, right? Or maybe I assumed (wrongly) that it will do so just because it's a major version?

@kumilingus
Copy link
Contributor Author

No. It will be included in v3.7.6 (September).
Until v4.0 is released, you can enable it at your own risk.
In v4 we will enable it by default and publish a changelog, migration guide, and add type definition and documentation.

@kumilingus kumilingus requested a review from zbynekstara October 2, 2023 09:13
@@ -1378,6 +1381,83 @@ const V = (function() {
return xml;
};

const _attributeNames = Object.create(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#null-prototype_objects

Do we need to use a null-prototype object here? It can apparently lead to unexpected behavior, which may be problematic (e.g. if a customer tries to modify the list of attributes for which not to split camel-case).

I think we should be able to use a normal object here, since we are then using a Proxy object anyways to specify the custom getter (line 1444).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What problems specifically?

This:

V('rect', { toString: true }).node

Should produce:

<rect to-string="true"/>

And it would not if we define _attributeNames as {}.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not questioning that part, but my point is that the {} which would be used as a replacement would be a normal Object (i.e. with an Object prototype) - so if using that is okay, then it should be okay to say const _attributes = {} on this line by default, as well.

Examples of potential problems of using null-prototype objects according to the MDN link:

  • An object with a null prototype can behave in unexpected ways, because it doesn't inherit any object methods from Object.prototype. This is especially true when debugging, since common object-property converting/detecting utility functions may generate errors, or lose information (especially if using silent error-traps that ignore errors).
  • The main problem problem is with missing toString, valueOf, hasOwnProperty functions and the constructor property

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment explaining why we use an object without prototype.
Note It's a private variable and can not be accessed from outside.

@kumilingus kumilingus marked this pull request as ready for review October 8, 2023 11:30
@jamesgeorgewilliams jamesgeorgewilliams merged commit 2c4804e into clientIO:master Oct 9, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants