-
Notifications
You must be signed in to change notification settings - Fork 635
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
Namespaced SVG function breaks existing classes in SVGs #3337
Comments
The point is to avoid conflicts between the SVG’s classes (and IDs, which the function already was namespacing before that update) and other elements on the page. It updates the class names and their references, so in theory shouldn’t actually have an effect on the visual appearance of the SVG. I may have overlooked something though; please upload your affected SVG image and I can look into it. |
Here’s a basic example I ran into just this morning. I have an SVG like this (the real one is more complicated, but you’ll get the idea):
On certain pages, I override the fill of the letters with something like:
This got broken because the
So the selector no longer matches, and I end up with dark letters on a dark background instead of the intended white letters on a dark background. I can work around this particular problem with attribute selectors, but I object to the behavior here. Craft should not be screwing around with attribute values like this, at least not by default. Anyone who did anything vaguely resembling what I did will have their CSS broken by it, as jhealey5 did. As he said, CSS-driven animations will be especially vulnerable to this, as will anyone’s attempt to style an SVG by referencing its ID. |
Followup: I’d also like to understand the rationale here, since I assume this is driven by a different sort of need. Perhaps we can work out a path (pun totally intended) that satisfies everyone! |
Ah interesting… @jhealey5 are you referencing SVG classes in separate @meyerweb The reason the function does this is because SVGs tend to be generated by apps like Illustrator or Sketch, which tend to reuse the same class names and IDs for the nodes. If you have a page that includes multiple SVGs generated by the same app, there’s a good chance that their classes/IDs will conflict with each other, so styles that were defined “in” SVG A and meant for nodes within that, may end up applying to nodes within SVG B as well. Namespacing the IDs and class names helps avoid that. (I put “in” in quotes because once you embed an SVG into HTML, it’s really just part of the rest of the HTML doc, which is why the styles can end up overreaching). Anyway, clearly there needs to be a way to disable the auto-namespacing. I’ll add a parameter to do just that… |
…and done. As of the next release you’ll be able to set {{ svg('@webroot/path/to/image.svg', namespace=false) }} If you’re comfortable with Terminal/Composer, you can get this change right away by updating your "require": {
"craftcms/cms": "dev-develop#ae61eb2d9020ff0d028b6e81bdbc611cd641734c as 3.0.26.1",
"...": "..."
}, Then go to your project in Terminal and run |
@brandonkelly Thanks for the fix! I primarily manually add classes to svgs for animations, so I can target paths/etc with JS/CSS, and/or overwrite things on certain pages. Though I second what @meyerweb said, whilst it is a nice idea, I don't think it's Crafts place to change SVG's, any duplicate attributes should probably be handled by the developer (or Sketch needs to sort out namespacing). That being said, I've not seen Sketch use classes at all, just ID's for referencing and styling. |
@jhealey5 The |
@brandonkelly Ah that makes sense, I didn't think of user uploaded svgs. Nicely done then (now we have an optional parameter) :) |
Thought a bit more about this and came up with a better solution. Now if a file path (or an alias) is passed in, the |
Hm, interesting. Given this, will the default of |
@meyerweb It changes to cms/src/web/twig/Extension.php Lines 818 to 830 in 2bfdad1
You can still explicitly enable/disable namespacing by adding |
- Issue where explicit use of a namespace variable in the svg function failed due to a recent security update craftcms/cms#3337
I do a lot of animation with svgs, the recent craft update broke lots of things because classes have now got namespaces https://github.com/craftcms/cms/blob/develop/CHANGELOG-v3.md#3026---2018-09-29
It needs to make sure to not interfere with any existing classes (add a space).
What is the reasoning behind it in the first place? Why would those namespaced classes ever get used?
The text was updated successfully, but these errors were encountered: