svgson.stringify should serialize object or array types for data attribute #8
Comments
Could you please give an example of complex data types in the svgson Object you need to stringify? not sure I catch what you mean 🙏 |
Here is the SVG content: <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="602" height="402" viewBox="0 0 602 402">
<g fill="none" fill-rule="nonzero" stroke="none" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" stroke-dasharray="" stroke-dashoffset="0" font-family="sans-serif" font-weight="normal" font-size="12" text-anchor="start" style="mix-blend-mode: normal" />
<g fill="#000000" fill-rule="nonzero" stroke="none" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" stroke-dasharray="" stroke-dashoffset="0" font-family="none" font-weight="none" font-size="none" text-anchor="none" style="mix-blend-mode: normal">
<path d="M37.79528,78.08504v-40.28976h31.67244v40.28976z" data-paper-data="{&quot;project&quot;:{&quot;layer&quot;:&quot;scripting&quot;}}" />
<path d="M86.81575,60.25516v-3.84h11.52756v3.84z" data-paper-data="{&quot;project&quot;:{&quot;layer&quot;:&quot;scripting&quot;}}" />
<path d="M86.81575,53.8224v-3.84h11.52756v3.84z" data-paper-data="{&quot;project&quot;:{&quot;layer&quot;:&quot;scripting&quot;}}" />
<path d="M86.81575,47.38965v-3.84h11.52756v3.84z" data-paper-data="{&quot;project&quot;:{&quot;layer&quot;:&quot;scripting&quot;}}" />
<path d="M86.81575,66.68791v-3.84h11.52756v3.84z" data-paper-data="{&quot;project&quot;:{&quot;layer&quot;:&quot;scripting&quot;}}" />
<path d="M86.81575,73.12067v-3.84h11.52756v3.84z" data-paper-data="{&quot;project&quot;:{&quot;layer&quot;:&quot;scripting&quot;}}" />
</g>
<g fill="none" fill-rule="nonzero" stroke="none" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" stroke-dasharray="" stroke-dashoffset="0" font-family="sans-serif" font-weight="normal" font-size="12" text-anchor="start" style="mix-blend-mode: normal" />
</svg> You can see there is a
transformNode = (node) ->
if node.attributes["data-paper-data"]
node.attributes["data-paper-data"] = JSON.parse htmlDecode that
node
json <~ svgson.parse _svg, {transformNode} .then
console.log JSON.stringify json, null, 2
...which produces JSON output correctly (unrelated parts are removed for clarity) {
"name": "svg",
...
"children": [
{
...
"children": [
{
"name": "path",
"type": "element",
"value": "",
"attributes": {
"d": "M37.79528,78.08504v-40.28976h31.67244v40.28976z",
"data-paper-data": {
"project": {
"layer": "scripting"
}
}
},
...
}
]
} Now we can manipulate the AST as we wish, add/remove some properties, and re-generate SVG: svg = svgson.stringify json Here we end up with buggy SVG: (unrelated parts are removed for clarity) ...
<path d="M37.79528,78.08504v-40.28976h31.67244v40.28976z" data-paper-data="[object Object]" />
<path d="M86.81575,60.25516v-3.84h11.52756v3.84z" data-paper-data="[object Object]" />
... Here you can see the complex data types are not stringified correctly. Current workaroundCurrently I'm stringifying those |
Thanks @ceremcem now I get it! In fact the parser is not responsible for what is inside each attr/prop, and as you shown you could achieve this using If I undertsand your current workaround is that you add a middle step looping nodes to
Do you think is responsability of Same situation in the Interested in analyse if could be a feature or some thing to explain in docs. |
On second thought, as we both agree that parsing a However, no data should be eaten up by
Yes, and it needs to be recursive. Although it's not hard to implement such a middle step, it reduces readability of resulting code. So my proposal boils down to those 2 features:
|
sound good your both proposals for That means, make a little tweaks on |
You could try in In tests there is an example with a similar situation you described above. Let me know, |
FYI: I still couldn't have a time to test this, but it's on my mind. |
no problem @ceremcem, let me know if there are issues, keep in mind that everything was moved into svgson@3.x so you could also update your deps. |
FYI: Nicely works! Thanks! |
Awesome, thanks for the feedback |
When we need to stringify complex data types (such as
Object
orArray
) indata-
attributes, those values should be stringified before converting to SVG.Alternatively,
svgson.stringify
might accept atransformNode
like option where we could manually convert any data format with a custom function.The text was updated successfully, but these errors were encountered: