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

[Bug/Question] StyleManager - SectorsView - addToCollection() uses "name" attribute to build the ID and fails with several spaces or special characters #1753

Closed
arachnosoft opened this issue Jan 24, 2019 · 3 comments
Assignees
Labels

Comments

@arachnosoft
Copy link

arachnosoft commented Jan 24, 2019

Hi @artf ,

Using the pluginOpts property from the grapesjs.init() method, I customized the sectors displayed on the Style Manager (through the Newsletter plugin), like this:

grapesjs.init({
[...]
            plugins: ['gjs-plugin-ckeditor', 'gjs-blocks-basic', 'gjs-preset-newsletter', 'gjs-preset-webpage'],
            pluginsOpts: {
[...]
'gjs-preset-newsletter': {
                    styleManagerSectors: [
                        {
                            id: "dimensions",
                            name: "My Custom Label With Spaces"
                            buildProps: [
                              [...]
                            ],
[...]
}
]
}
}
);

The goal being to translate the labels into another language.

But I found that the "name" property is also used... to build the id attribute of each sector's HTML tag, with a custom prefix, like "gjs-".

The problem being when "name" contains several spaces: only the first one is being replaced, leading to an invalid HTML id attribute (with spaces), which can't be targeted through CSS to apply some custom styling on the Style Manager panel (see attached screenshot).

I opened grapes.js, and found this in the addToCollection() method:

    var view = new SectorView({
      model: model,
      id: this.pfx + model.get('name').replace(' ', '_').toLowerCase(),
      name: model.get('name'),
      properties: model.get('properties'),
      target: this.target,
      propTarget: this.propTarget,
      config: this.config
    });

As you may know, JavaScript's standard "replace" method is acting kinda weird by default, replacing only the first occurrence in the source string, if you don't explicitely set a RegExp with the g flag (unlike all other programming languages available on Earth, which take care of this automatically). Hence the bug.

Moreover, the "name" attribute can also contain special (accentuated) characters in some languages, and they could also break the ID as well, as you don't escape them.

If using the "name" attribute is intentional here, and not a copy/paste mistake (in case you meant model.get('id') instead of model.get('name')), what's the purpose of this?
Rather than using, say, an "id" property we could define ourselves? (and naturally avoiding to use any spaces or special characters with a such-named property).

You could use the "id" attribute if defined, and fallback to your current "name" attribute conversion if necessary (taking care to replace ALL spaces).

Therefore, I changed this line in grapes.js:
id: this.pfx + model.get('name').replace(' ', '_').toLowerCase(),
with:
id: this.pfx + model.get('id'),

And, in my code:
styleManagerSectors: [ { id: "dimensions", name: "My Custom Label With Spaces", [...] } ]

So my sectors' ID is now "gjs-sm-dimensions" as expected, and not "gjs-sm-my_custom label with spaces"

Subsidiary question: is it dangerous to keep this change while waiting for an official fix (or not) from you?

Thanks for your opinion.

-Maxime

@arachnosoft
Copy link
Author

Here is the screenshot I mentioned earlier:

grapesjs id attribute bug report

@artf
Copy link
Member

artf commented Jan 27, 2019

If using the "name" attribute is intentional here, and not a copy/paste mistake (in case you meant model.get('id') instead of model.get('name')), what's the purpose of this?
Rather than using, say, an "id" property we could define ourselves? (and naturally avoiding to use any spaces or special characters with a such-named property).

Eh definitely not a wise choice, probably I was not yet using ids on that models. I'll change it in the next release.

Subsidiary question: is it dangerous to keep this change while waiting for an official fix (or not) from you?

I think, to make it more customize friendly, it would be a case to remove that id attribute and add a new class instead, so the final result should be class="gjs-sm-sector gjs-sm-sector__MYID"

@artf artf self-assigned this Jan 27, 2019
@artf artf added this to To do in Release v0.14.55 via automation Jan 27, 2019
@artf artf moved this from To do to In progress in Release v0.14.55 Jan 27, 2019
@artf artf closed this as completed in 3079efb Jan 27, 2019
Release v0.14.55 automation moved this from In progress to Done Jan 27, 2019
@lock
Copy link

lock bot commented Jan 27, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot added the outdated label Jan 27, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Jan 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
No open projects
Development

No branches or pull requests

2 participants