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

Remove fill and stroke unless "none" #440

Closed
ccorcos opened this issue Oct 11, 2015 · 12 comments
Closed

Remove fill and stroke unless "none" #440

ccorcos opened this issue Oct 11, 2015 · 12 comments

Comments

@ccorcos
Copy link

ccorcos commented Oct 11, 2015

when I'm styling my SVG's with CSS its nice to strip all the inline colors so I can style them myself. This challenging when colors are set with inline styles. So I strip them out:

  {
    removeAttrs: {attrs: '(stroke|fill)'},
  }

But then there are circles that are filled in when they shouldnt be. So it would be aweosme if you could strip all the stroke and fill attributes so long are they're not none. Unless you've found a better way of dealing with this...

@ccorcos
Copy link
Author

ccorcos commented Oct 11, 2015

{ removeAttrs: {attrs: '(stroke|fill)'} }, is also removing stroke-width

@GreLI
Copy link
Member

GreLI commented Oct 12, 2015

Sorry, I don't understand your first statement.

{ removeAttrs: {attrs: '(stroke|fill)'} }, is also removing stroke-width

It's likely that removeUselessStrokeAndFill removes stroke-width since there is no stroke. Just disable it.

@ccorcos
Copy link
Author

ccorcos commented Oct 12, 2015

Ah. I'll try that.

Let me reiterate my first statement:

When using these svg's inline an styling with CSS, its nice to be able to set the stroke and fill colors. Thus, I want to remove the stroke and fill colors. However I want to preserve all styles where fill is set to none -- that is, I want to preserve hallow regions. Stripping all fill attributes will cause icons to look wrong.

@GreLI
Copy link
Member

GreLI commented Oct 12, 2015

Well, removeAttrs takes only style attributes names. You can extend it if you want, but this is not main svgo purpose.

@ccorcos
Copy link
Author

ccorcos commented Oct 13, 2015

ok. I see. SVG's are kind of a pain.... :/

@ccorcos ccorcos closed this as completed Oct 13, 2015
@KingScooty
Copy link

This is actually a valid use case. I've just come across it myself. It'd be nice to strip out all color and fill refs if they aren't set to "none". We need to keep "none" as it preserves some important styling.

SVGO is an optimizer, and optimizing for the web should be a valid use case.

Being able to set the colour of SVGs using CSS is a huge plus, and this can't be done if there's stray fills or colours in the file.

@ekfuhrmann
Copy link

ekfuhrmann commented Dec 13, 2017

I agree that there is a valid use case for this.

Lets say you have an icon library with a consistent container size, such as 24 x 24 px, you'll sometimes have icons within that container that are smaller than it for visual continuity -- so maybe an 18 x 18 px icon within the 24 x 24 px container.

This normally wouldn't be an issue so long as you can leave the container path to fill="none", but since it's being stripped out, it makes such a system obsolete.

While I know I can prevent fill from being stripped, it's nice to have so that you can control the icon color via CSS.

A proposed solution would be something along the lines of attrs: ['stroke.*', 'fill.*!none'] which would remove all fill attributes from the SVG aside for those that have a value of none.

@ailet37
Copy link

ailet37 commented May 23, 2018

is there a way today to remove all fill except none value ?

@ekfuhrmann
Copy link

@ailet37 I still haven't been able to figure out a solution to removing all fill except none. I've for the most part removed this part from my build process and now just manually clean the fills whenever I need a fill: none preserved.

It's not great and I wish the owner would reconsider adding this functionality since it has numerous valid implementations.

@Herman-Freund
Copy link

Herman-Freund commented Jun 11, 2018

@GreLI @ccorcos @KingScooty @ekfuhrmann @ailet37

I've opened pull request #977 that addresses this issue.

Proposed solution:

{
  removeAttrs: {
    attrs: '*:(stroke|fill):((?!^none$).)*'
  }
}

@Herman-Freund
Copy link

This has now been resolved and merged via #977

@GreLI
Copy link
Member

GreLI commented Feb 25, 2019

Should work in SVGO v1.2.0.

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

No branches or pull requests

6 participants