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

Add prefixIds plugin #700

Merged
merged 27 commits into from
Oct 23, 2017
Merged

Add prefixIds plugin #700

merged 27 commits into from
Oct 23, 2017

Conversation

strarsis
Copy link
Contributor

@strarsis strarsis commented Apr 5, 2017

This PR adds the prefixIds plugin which prefixes all identifiers (IDs, class names) and
also updates the url(...) references.

Additionally support for extra information is added that can be optionally used by the plugins,
currently containing the path to the input SVG file or that mode is text (when text input is used).

Related issues: #494 #595 #623 #673 #674
Similar approach would be inlining all styles (see the inlineStyles plugin) -
though this is not possible with elements that are <used/> (see #651).
This prefixing approach could be viewed as a "polyfill" (partially) for scoped styles.

Add dom-walker file.
Add tests.
Add dependencies to package file.
strarsis added 2 commits April 5, 2017 19:59
Fix skipping in loop.
Add missing 'href' to condition.
@strarsis strarsis changed the title prefixIds plugin Add prefixIds plugin Apr 5, 2017
@@ -20,7 +20,7 @@ var SVGO = module.exports = function(config) {
this.config = CONFIG(config);
};

SVGO.prototype.optimize = function(svgstr) {
SVGO.prototype.optimize = function(svgstr, info) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why you don't pass that info data in the config?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The svgo architecture handles the config globally, it is harder to add per-plugin extra config than adding an extra argument to each plugin. Also this extra information is not config but rather environment/state instead.

Copy link
Contributor

@caub caub Apr 6, 2017

Choose a reason for hiding this comment

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

good point, things like floatPrecision too, could be global instead of spread like now https://github.com/svg/svgo/blob/master/lib/svgo/config.js#L36 I'll see if it could be done with your way.

But if you want to do less changes, you could do it like ^, every plugin that needs a global config, defines it in the config, and it gets spread

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the file path must be somehow injected into the config object for each file (or set to empty/false/null for a string input): https://github.com/strarsis/svgo/blob/67e5a3ef10e349bded15bc9f508596252c2884be/lib/svgo/coa.js#L437

Copy link
Contributor

Choose a reason for hiding this comment

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

yea sorry again to be annoying, after that I stop:), but I think the best is to have an optional prefix property at the root of the config and pass it to any plugin that needs it like https://github.com/svg/svgo/blob/master/lib/svgo/config.js#L34, it's more consistent with current code. The other option would be to refactor more and do var svgo = require('svgo'); svgo.optimize(svgStr, options); passing whole config at each call, possibly in the form {common: {floatPrecision:.., prefix: '..'}, plugins: [...]} so each plugin receive common and its plugin specific options. This plugin is interesting, even if it doesn't really minify, I use svgo to make png/jpg previews of them, and I do that in iframes to avoid those conflicts, so your way could make it work too.

Copy link
Contributor Author

@strarsis strarsis Oct 22, 2017

Choose a reason for hiding this comment

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



var path = require('path'),
csstree = require('css-tree'),
Copy link
Contributor

@caub caub Apr 6, 2017

Choose a reason for hiding this comment

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

those 3 deps are really not necessary

just put the regex you need, and for tree walking it's just

function walk(node, cb) {
 cb(node);
 if (node.content) node.content.forEach(walk)
}

but make sure you can't use 'perItem' mode first

Copy link
Contributor Author

@strarsis strarsis Apr 6, 2017

Choose a reason for hiding this comment

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

Good point, I use perItem now.

@strarsis
Copy link
Contributor Author

strarsis commented Apr 6, 2017

Edit: Referenceable attribute check added.

Currently all attribute values are checked for url(...) / ID references.
Maybe this can be made more efficient by only checking attributes that can actually reference to other elements, as defined in https://github.com/svg/svgo/blob/master/plugins/_collections.js#L2286.
Edit: The list contains properties, not elements that can reference.


exports.type = 'perItem';

exports.active = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

also I wouldn"t enable it by default

strarsis added 2 commits April 7, 2017 15:27
Improve code.
Improve tests.
Clean up log.
@strarsis strarsis closed this Apr 7, 2017
@strarsis strarsis reopened this Apr 7, 2017
@strarsis strarsis closed this Jun 28, 2017
@strarsis strarsis reopened this Jun 28, 2017
@strarsis
Copy link
Contributor Author

strarsis commented Aug 7, 2017

FWIW, I just noticed that Illustrator CC offers 'Unique' for 'Object IDs' in SVG export options.

@funklos
Copy link

funklos commented Aug 8, 2017

Jeah we also found this option but we also noticed that illustrator export is not suitable in every case.
In cooperation with our graphics department we decided to use sketch for SVG exports, since sketch is gaining more and more traction for reponsive screen design this was the logical consequence.
So the combination of sketch with svgo for batch optimization on deployment is our way in the future..

@strarsis
Copy link
Contributor Author

strarsis commented Oct 22, 2017

@GreLI: Conflicts fixed, ready to merge!

@GreLI GreLI merged commit 91d9810 into svg:master Oct 23, 2017
@strarsis
Copy link
Contributor Author

👍 Yes!

@theKashey
Copy link

Awaiting for the version bump!

@chenxiaochun
Copy link

Very good!

@TongDaDa
Copy link

I want to cancel the class prefix in the svg file, What should I do?

@strarsis
Copy link
Contributor Author

strarsis commented Sep 2, 2020

@TongDaDa: You can disable plugins you don't want to use.

jherdman pushed a commit to evoactivity/ember-svg-jar that referenced this pull request Jan 13, 2022
in order to support prefixIds svgo rule.

@see: svg/svgo#700
jherdman pushed a commit to evoactivity/ember-svg-jar that referenced this pull request Jan 24, 2022
in order to support prefixIds svgo rule.

@see: svg/svgo#700
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.

9 participants