Details & Tips
The optimization pipeline runs four passes, in this order, directly on the SVG text: (a) strip every XML/HTML comment (`<!-- ... -->`); (b) strip `<metadata>...</metadata>` blocks entirely, along with any attribute whose name starts with `inkscape:` or `sodipodi:` (and their `xmlns:inkscape` / `xmlns:sodipodi` namespace declarations) — `<title>` and `<desc>` elements are deliberately kept, since they carry real accessibility and tooltip value, unlike purely editor-internal metadata; (c) round any numeric attribute value that already carries three or more decimal digits down to exactly two (so `12.345678` becomes `12.35`, while an already-short `12.5` is left untouched rather than needlessly padded to `12.50`) across the coordinate-bearing attributes `d`, `x`, `y`, `width`, `height`, `cx`, `cy`, `r`, `rx`, `ry`, `x1`, `y1`, `x2`, `y2`, `points`, `viewBox` and `stroke-width`; (d) collapse whitespace runs that sit purely between two tags (typical pretty-printed indentation) down to nothing.
That last step is worth walking through carefully, because getting it wrong would be a real correctness bug: the collapsing regex only ever matches a run of whitespace characters that sits between a `>` and the very next `<`, with *nothing else* in between. Given `<svg>\n <rect .../>\n <text>Hello World</text>\n</svg>`, the newline-and-indentation whitespace between `<svg>` and `<rect` is pure whitespace with nothing else present, so it collapses away cleanly. But the space between "Hello" and "World" inside `<text>Hello World</text>` is never touched, because it doesn't sit between a `>` and a `<` at all — it sits between two ordinary letters, entirely inside the text node's own content, which the collapsing pattern never even looks at. The one gap that *does* sit between tags here — right after `<text>` and right before `</text>`'s closing bracket, if the source had any surrounding whitespace there — would only collapse if that entire gap were whitespace; since "Hello World" occupies it, the pattern can't match across real content, and the visually meaningful space survives untouched.
Size reduction is measured with the browser's own `Blob` size in bytes (which correctly accounts for multi-byte UTF-8 characters, unlike a plain string-length count), for both the original and optimized text, and shown as a percentage saved. The live before/after preview renders both versions by inserting the markup into two separate preview containers — a deliberate, narrowly-scoped exception to this codebase's usual "never render user input via innerHTML" rule, made safe here specifically because it is the *same* user viewing their *own* just-pasted markup back in their *own* browser tab: no other user, page or origin is ever involved, so no privilege boundary is crossed, and rendering it as real SVG (rather than escaped text) is the entire point of a visual before/after comparison.
Worked example: a typical Inkscape export might include a leading XML comment block, a 200-byte `<metadata>` block with RDF authoring data, a dozen `inkscape:` and `sodipodi:` attributes scattered across path elements, path coordinates specified to 10+ decimal places, and two-space pretty-printed indentation throughout — a combination that routinely adds 30-60% to the file's size without changing a single visible pixel. Running that same file through this tool typically shows a meaningful percentage reduction while the before/after preview panels render pixel-identically, which is exactly the point: smaller file, same picture.