From 2deaf4710a4e6f2c99ce40bea8377e9773b3b9df Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Sun, 3 Jun 2018 02:44:57 -0700 Subject: [PATCH] Sort SVG attributes alphabetically. (#252) * Sort SVG attributes alphabetically. This makes the exported SVG deterministic and diffable. * Simplify attr variable --- editor/svgcanvas.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index de949cff..ecbfe9bc 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -2968,9 +2968,10 @@ this.svgToString = function (elem, indent) { if (elem) { cleanupElement(elem); - const attrs = elem.attributes; - let attr, i; + const attrs = Array.from(elem.attributes); + let i; const childs = elem.childNodes; + attrs.sort((a, b) => a.name > b.name ? -1 : 1); for (i = 0; i < indent; i++) { out.push(' '); } out.push('<'); out.push(elem.nodeName); @@ -3022,7 +3023,7 @@ this.svgToString = function (elem, indent) { i = attrs.length; const attrNames = ['width', 'height', 'xmlns', 'x', 'y', 'viewBox', 'id', 'overflow']; while (i--) { - attr = attrs.item(i); + let attr = attrs[i]; const attrVal = toXml(attr.value); // Namespaces have already been dealt with, so skip @@ -3043,7 +3044,7 @@ this.svgToString = function (elem, indent) { const mozAttrs = ['-moz-math-font-style', '_moz-math-font-style']; for (i = attrs.length - 1; i >= 0; i--) { - attr = attrs.item(i); + let attr = attrs[i]; let attrVal = toXml(attr.value); // remove bogus attributes added by Gecko if (mozAttrs.includes(attr.localName)) { continue; }