Svelte: Rehydrating SVG: Cannot assign to read only property

Created on 10 May 2019  路  7Comments  路  Source: sveltejs/svelte

Uncaught (in promise) TypeError: Cannot assign to read only property 'width' of object '#<SVGSVGElement>'

Most helpful comment

Fixed in 3.10.1 (finally!)

All 7 comments

It seems like this occurs when there are spread attributes with read-only props on SVGSVGElement

https://svelte.dev/repl/759086cf15ec4220ac67ab039e1388d2?version=3.2.2

If set_attributes is changed to only use setAttribute instead of directly assigning a new value on an existing node property as an experiment, it works as expected.

export function set_attributes(node, attributes) {
  for (const key in attributes) {
    if (key === "style") {
      node.style.cssText = attributes[key];
-   } else if (key in node) {
-     node[key] = attributes[key];
    } else {
      attr(node, key, attributes[key]);
    }
  }
}

I'm not sure what's the best way forward.

@EmilTholin Some tests failed when removing node[key] = attributes[key];, so I used a try/catch in the PR...

export function set_attributes(node, attributes) {
    for (const key in attributes) {
        if (key === 'style') {
            node.style.cssText = attributes[key];
        } else if (key in node) {
            try {
                node[key] = attributes[key];
            } catch (e) {
                attr(node, key, attributes[key]);
            }
        } else {
            attr(node, key, attributes[key]);
        }
    }
}

I have not figured out how to create a failing test for this scenario...other than the repl.

Pinging this issue. I just realized that if this fix would allow a preprocessor for *.svg files to be svelte components with overridable attributes.

In this example, the svg:

<svg viewBox="0 0 512 512" stroke="#D9534F">
<!--...-->
</svg>

would generate:

<svelte:options namespace="svg"></svelte:options>
<svg viewBox="0 0 512 512" stroke="#D9534F" {...$$props}>
<!--...-->
</svg>

The component would have a default stroke of #D9534F. This stroke can be overridden with the stroke attribute passed to the component.

<SVG_Component stroke="#00FF00"></SVG_Component>

Fixed in 3.10.1 (finally!)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

juniorsd picture juniorsd  路  3Comments

davidcallanan picture davidcallanan  路  3Comments

mmjmanders picture mmjmanders  路  3Comments

sskyy picture sskyy  路  3Comments

noypiscripter picture noypiscripter  路  3Comments