Method-Draw icon indicating copy to clipboard operation
Method-Draw copied to clipboard

Attribute xlink:href redefined

Open kamshory opened this issue 2 years ago • 1 comments

When I place a PNG image in the document, it throws an error "Attribute xlink:href redefined". When I check the tag, there is a duplicate attribute xlink:href. How to get rid of the duplication?

kamshory avatar Jan 31 '23 12:01 kamshory

The Problem

I was able to pin down the issue to line 5355 in src/js/svgcanvas.js.

if (illutratorCompat && str.includes(" href=")) str = str.replace(" href=", " xlink:href=");

If the xml already has an xlink:href attribute AND an href attribute, the href becomes xlink:href.

SVG2 removes the xlink namespace altogether, and this attribute is now deprecated.

The "real solution" to this would be to figure out how to stop xlink:href from being written in the first place. (I mean, if backwards-compatibility isn't an issue for you) Oddly enough, removing this line

svgedit.utilities.setHref = function(elem, val) {
  elem.setAttributeNS(XLINKNS, "xlink:href", val); // this
  elem.setAttribute("href", val);
}

and/or this line

    image.setAttributeNS(xlinkns,'xlink:href',url);

doesn't solve it.

Working (but not ideal) Solution

Consider a PR that changes that line to

// make sure we don't already have an `xlink:href` attribute
if (illutratorCompat && str.includes(" href=") && !str.includes(" xlink:href=")) {
   str = str.replace(" href=", " xlink:href=");
  }

or simply "if it has both attributes don't do anything".

It solves the problem but feels hacky and wrong. Plus it might break other workflows?

So currently the entire image Base64 is written twice... once in the href attr and the other in the xlink namespace attr. the xlink one can obviously be removed in the line after, but it's best not to create it at all.

julles007 avatar Feb 27 '23 23:02 julles007