-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Convert <use> tags. By default only defs with a single <use>. #670
base: main
Are you sure you want to change the base?
Conversation
[Edit: My test was faulty.] |
There are several caveats in your code. |
I don't think this PR handles CSS cascades correctly. According to the SVG spec, CSS cascade is applied to the referenced elements, not the (conceptually) cloned elements that make up the shadow DOM. What I think you would have to do is:
Also I would take a look at the SVG spec on the Also this plugin doesn't handle the case where the |
Well, SVGO doesn't handle CSS almost at all. |
A window.getComputedStyle() implementation would be useful here. |
|
||
if (item.isElem('use') && item.hasAttr('xlink:href')) { | ||
|
||
var id = item.attr('xlink:href').value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xlink
can has any prefix not always xlink
. Moreover, SVG 2 has allowed using href
attribute without prefix at all.
// Make sure we don't call properties of undefined | ||
var prevId, nextId; | ||
try { prevId = itemAry[i-1].attr('xlink:href').value; } | ||
catch (e) { prevId = null; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just set
prevId = i > 0 ? itemAry[i - 1].attr('xlink:href').value : null;
nextId = i < itemAry.length - 1 ? itemAry[i + 1].attr('xlink:href').value : null;
without try-catch
(they are preventing JIT-optimizing).
But I'd recommend using Set
to check for uniqueness.
<rect id="rect2" x="40" y="40" width="140" height="120" rx="3" fill="green"/> | ||
</g> | ||
<g stroke="blue" fill="none" stroke-width="2"> | ||
<rect id="rect" x="0" y="0" width="200" height="200"/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are two elements with the same id="rect"
. Worth removing id?
I ran into exported svg's that use a lot of unnecessary defs, and decided to take matters into my own hands...
This plugin:
<defs>
element definitions. It then finds all<use>
items.(In hindsight, this should have been done in a single loop.)
<use>
to<g>
and inserts a clone of the definition.This plugin does not remove unused definitions or collapse the resulting groups, leaving that for
removeUselessDefs
andcollapseGroups
.I believe this plugin would cover the request in #563
[edit: now removes the definitions that have been inlined]