Create unique ID prefix within SVGs to prevent ID collisions#108
Create unique ID prefix within SVGs to prevent ID collisions#108SilverFox70 wants to merge 18 commits intojhamlet:masterfrom
Conversation
Added fix and test cases for paths names including consecutive dashes…
Feature/create unique id prefix
chore: remove logging line
feat: add another test and update variable name in test file
Feat/add test
woutervanvliet
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
You probably don't want your "this fork" mentions merged into the main package.
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" |
There was a problem hiding this comment.
Also probably not a change that should be merged up ;)
| /*globals describe, it*/ | ||
| require('should'); | ||
|
|
||
| describe('svg-react-loader/lib/sanitize/filters/unique-svg-ids', () => { |
There was a problem hiding this comment.
Maybe you can also include a simple test in test/integration/test.js on an actual svg file.
| var raw = params.raw; | ||
| var xmlnsTest = params.xmlnsTest; | ||
| var classIdPrefix = params.classIdPrefix || false; | ||
| var uniqueIdPrefix = params.uniqueIdPrefix || false; |
There was a problem hiding this comment.
Would it make sense to use the value, after interpolation, of classIdPrefix as a default?
There was a problem hiding this comment.
(interpolation will be fixed as soon as #107 is merged, btw)
There was a problem hiding this comment.
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 |
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" |
| /*globals describe, it*/ | ||
| require('should'); | ||
|
|
||
| describe('svg-react-loader/lib/sanitize/filters/unique-svg-ids', () => { |
| this.update(value); | ||
| } | ||
|
|
||
| // Find all IDs and update with filename prefix |
There was a problem hiding this comment.
Couple of thoughts:
-
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, ?...
-
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
testproperty, and then aoperatorfunction 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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)"
There was a problem hiding this comment.
@SilverFox70 created pull request to resolve this issue SilverFox70#5
| })); | ||
| } | ||
|
|
||
| if (options.uniqueIdPrefix) { |
There was a problem hiding this comment.
This option sounds and feels like it should default to true
any reasons for not making it default to being on?
There was a problem hiding this comment.
Yes, I would agree with that. Super busy today, but if I can carve out ten minutes I can update that.
|
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 |
|
Is there anything I can do to help get this feature merged? |
|
I just added the last change - to make uniqueIdPrefix default to true. |
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.