Skip to content

Commit

Permalink
Don't add container when inlining non-symbol.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkenny54 committed Oct 17, 2024
1 parent 6c4576b commit e0ff0e4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
28 changes: 23 additions & 5 deletions plugins/inlineUse.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function inlineUse(use, def) {
const useProperties = getPresentationProperties(use);

// Overwrite <use> properties with def properties.
if (defProperties && isContainer) {
if (defProperties) {
for (const [propName, propValue] of defProperties.entries()) {
useProperties.set(propName, propValue);
}
Expand All @@ -161,8 +161,8 @@ function inlineUse(use, def) {
useProperties.delete('transform');
}

// Convert the <use> to <g>.
use.name = 'g';
// Convert the <use>.
use.name = isContainer ? 'g' : def.name;

// Update attributes.
let tx = '0';
Expand All @@ -179,16 +179,34 @@ function inlineUse(use, def) {
delete use.attributes[attName];
}

// Copy any non-presentation properties from def.
for (const [attName, attValue] of Object.entries(def.attributes)) {
switch (attName) {
case 'id':
case 'overflow':
case 'style':
case 'transform':
case 'transform-origin':
continue;
default:
if (!useProperties.has(attName)) {
use.attributes[attName] = attValue.toString();
}
break;
}
}

// Add translation if necessary.
if (tx !== '0' || ty !== '0') {
transform = transform + `translate(${tx},${ty})`;
const translate = `translate(${tx},${ty})`;
transform = isContainer ? transform + translate : translate + transform;
}
if (transform !== '') {
use.attributes.transform = transform;
}
writeStyleAttribute(use, useProperties);

use.children = isContainer ? def.children : [def];
use.children = def.children;
use.children.forEach((c) => (c.parentNode = use));

return true;
Expand Down
4 changes: 1 addition & 3 deletions test/plugins/inlineUse.06.svg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@ Inline path element.

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10">
<defs/>
<g transform="translate(2,2)">
<path id="a" d="M1 1L3 3" style="stroke:black"/>
</g>
<path d="M1 1L3 3" transform="translate(2,2)" style="stroke:black"/>
</svg>
17 changes: 17 additions & 0 deletions test/plugins/inlineUse.07.svg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Inline path element with transform.

===

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10">
<defs>
<path id="a" d="M1 1L3 3" transform="scale(2)" style="stroke:black"/>
</defs>
<use x="2" y="2" href="#a"/>
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10">
<defs/>
<path d="M1 1L3 3" transform="translate(2,2)scale(2)" style="stroke:black"/>
</svg>

0 comments on commit e0ff0e4

Please sign in to comment.