Skip to content
This repository was archived by the owner on Dec 11, 2025. It is now read-only.

Comments

Create unique ID prefix within SVGs to prevent ID collisions#108

Open
SilverFox70 wants to merge 18 commits intojhamlet:masterfrom
SilverFox70:master
Open

Create unique ID prefix within SVGs to prevent ID collisions#108
SilverFox70 wants to merge 18 commits intojhamlet:masterfrom
SilverFox70:master

Conversation

@SilverFox70
Copy link

Haven't had time to go back over this with a fine tooth comb since I made these changes, but it is working fine on two separate production sites at this point. It could be made more robust by implementing a 'usable' default prefix based on something like shortid, but that would meaning adding dependencies: see lib/sanitize/filters/unique-svg-ids.js at line line 7 for more info.

Copy link

@woutervanvliet woutervanvliet left a comment

Choose a reason for hiding this comment

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

So good to see a fix for this to be pending review ;).

I'm not an SVG expert, but your filter looks fine to me. PR would need to be cleaned for all "fork" references though.

Note: I'm neither contributor nor maintainer for this project, just passing by and hoping to get this PR merged asap as I need it myself.

README.md Outdated
are acted on by the loader version of `svg-react`.
are acted on by the loader version of `svg-react`.

This fork has an added filter which creates 'unique' IDs and mask, fill, and xlink:href

Choose a reason for hiding this comment

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

You probably don't want your "this fork" mentions merged into the main package.

Copy link
Owner

Choose a reason for hiding this comment

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

+1

package.json Outdated
"repository": {
"type": "git",
"url": "git+https://github.com/jhamlet/svg-react-loader.git"
"url": "git+https://github.com/SilverFox70/svg-react-loader"

Choose a reason for hiding this comment

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

Also probably not a change that should be merged up ;)

Copy link
Owner

Choose a reason for hiding this comment

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

+1

/*globals describe, it*/
require('should');

describe('svg-react-loader/lib/sanitize/filters/unique-svg-ids', () => {

Choose a reason for hiding this comment

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

Maybe you can also include a simple test in test/integration/test.js on an actual svg file.

Copy link
Owner

Choose a reason for hiding this comment

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

+1

var raw = params.raw;
var xmlnsTest = params.xmlnsTest;
var classIdPrefix = params.classIdPrefix || false;
var uniqueIdPrefix = params.uniqueIdPrefix || false;

Choose a reason for hiding this comment

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

Would it make sense to use the value, after interpolation, of classIdPrefix as a default?

Choose a reason for hiding this comment

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

(interpolation will be fixed as soon as #107 is merged, btw)

Copy link
Owner

Choose a reason for hiding this comment

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

I'm not sure if that would work as intended.

Using the same prefix would across all instances and files would collide too, no?

It might be more relevant to allow tokenizing within a default string, a la: [name]-[hash]-

README.md Outdated
are acted on by the loader version of `svg-react`.
are acted on by the loader version of `svg-react`.

This fork has an added filter which creates 'unique' IDs and mask, fill, and xlink:href
Copy link
Owner

Choose a reason for hiding this comment

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

+1

package.json Outdated
"repository": {
"type": "git",
"url": "git+https://github.com/jhamlet/svg-react-loader.git"
"url": "git+https://github.com/SilverFox70/svg-react-loader"
Copy link
Owner

Choose a reason for hiding this comment

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

+1

/*globals describe, it*/
require('should');

describe('svg-react-loader/lib/sanitize/filters/unique-svg-ids', () => {
Copy link
Owner

Choose a reason for hiding this comment

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

+1

this.update(value);
}

// Find all IDs and update with filename prefix
Copy link
Owner

Choose a reason for hiding this comment

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

Couple of thoughts:

  1. Are these the only attributes that need to be sanitized for the id prefix? I can think of (at least) a couple more: clip-path, use, ?...

  2. There is a bit of repetition going on here for some of the attributes... you might be able to break this down to an array of objects with a test property, and then a operator function property which takes the node reference and other parameters it may need (prefixId, etc...).

By abstracting the update away into separate functions, and operating over a list of definitions, it becomes trivial to handle new cases (just add them to the list).

Bonus Points: Allow for end users to configure their loader with additional tests (exporting the utility operator functions too).

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for looking this over and providing the feedback. I will endeavor to get back around to this and resolve as soon as I am able.

Copy link
Author

Choose a reason for hiding this comment

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

Totally agree about abstracting items into separate functions. As the old saying goes, "I would have written a shorter letter if I'd had more time". I was able to carve out a few minutes to add a couple of tests and continue the not-abstract, brute force methods for some expanded cases.

Perhaps I will have more time in the future to revisit, refactor, and make sure that all cases of ID reference are handled

Copy link

@anagami anagami Mar 25, 2019

Choose a reason for hiding this comment

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

Hi!
if i try insert svg with some custom filter

<svg xmlns="http://www.w3.org/2000/svg" width="16" height="22" viewBox="0 0 16 22">
    <defs>
        <filter id="a" width="200%" height="200%" x="-50%" y="-50%" filterUnits="objectBoundingBox">
            ....
        </filter>
    </defs>
    <g fill="none" fill-rule="evenodd" filter="url(#a)" transform="matrix(-1 0 0 1 19 -3)">
        ...
    </g>
</svg>

loader convert only <filter id="test__a", but reference to filter - original url <g filter="url(#a)"

Copy link

Choose a reason for hiding this comment

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

@SilverFox70 created pull request to resolve this issue SilverFox70#5

}));
}

if (options.uniqueIdPrefix) {
Copy link
Owner

Choose a reason for hiding this comment

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

This option sounds and feels like it should default to true

any reasons for not making it default to being on?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I would agree with that. Super busy today, but if I can carve out ten minutes I can update that.

@jhamlet
Copy link
Owner

jhamlet commented Oct 26, 2018

As far as I can see this is looking good.

I made a couple more comments about defaulting to having this feature on, instead of off.

I'll merge it some time over the weekend.

Thanks,

;-j

@jedrichards
Copy link

Is there anything I can do to help get this feature merged?

@SilverFox70
Copy link
Author

I just added the last change - to make uniqueIdPrefix default to true.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants