When I include a non-breaking space (character encoded as \u00A0
) in an SVG text
element, it works fine on a HTML page, but the resulting SVG contains an
character and is therefore not valid as an SVG file. For example, if I save the SVG to a file (using a function like this) and open that file in Chrome, it reports an error "Entity 'nbsp' not defined" and does not display the text.
const NS = { SVG: "http://www.w3.org/2000/svg" };
const svg = document.createElementNS(NS.SVG, 'svg');
const text = document.createElementNS(NS.SVG, "text");
text.setAttribute("y", 20);
const s = "Non-breaking> <space"; // the space here is a non-breaking one
text.appendChild(document.createTextNode(s));
svg.appendChild(text);
document.body.appendChild(svg);
console.log(svg.outerHTML);
I can easily replace the problematic character with s.replace("\u00A0", " ")
. But is there a more general way of sanitizing text going into an SVG to ensure it is valid and replacing the non-valid characters?