From 2235f81ee7e3bf927126de10de03e7ecdd396f60 Mon Sep 17 00:00:00 2001 From: cuixiping Date: Fri, 17 Jan 2020 15:34:00 +0800 Subject: [PATCH] Fix: remove "null" attributes in svg source #76 --- editor/svgcanvas.js | 9 +++++++-- editor/utilities.js | 9 ++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index 04edf2a9..a9f70f43 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -3744,6 +3744,10 @@ this.svgToString = function (elem, indent) { let attrVal = toXml(attr.value); // remove bogus attributes added by Gecko if (mozAttrs.includes(attr.localName)) { continue; } + if (attrVal === 'null') { + const styleName = attr.localName.replace(/-[a-z]/g, (s) => s[1].toUpperCase()); + if (Object.prototype.hasOwnProperty.call(elem.style, styleName)) { continue; } + } if (attrVal !== '') { if (attrVal.startsWith('pointer-events')) { continue; } if (attr.localName === 'class' && attrVal.startsWith('se_')) { continue; } @@ -3786,7 +3790,7 @@ this.svgToString = function (elem, indent) { switch (child.nodeType) { case 1: // element node out.push('\n'); - out.push(this.svgToString(childs.item(i), indent)); + out.push(this.svgToString(child, indent)); break; case 3: { // text node const str = child.nodeValue.replace(/^\s+|\s+$/g, ''); @@ -4072,7 +4076,8 @@ this.exportPDF = async function ( // We do not currently have these paths configurable as they are // currently global-only, so not Rolled-up 'jspdf/underscore-min.js', - 'jspdf/jspdf.min.js' + // 'jspdf/jspdf.min.js', + '../../svgedit-myfix/editor/jspdf/jspdf-1.0.150.debug.js' ]); const modularVersion = !('svgEditor' in window) || diff --git a/editor/utilities.js b/editor/utilities.js index 8d0086b2..ecd1985e 100644 --- a/editor/utilities.js +++ b/editor/utilities.js @@ -1237,7 +1237,14 @@ export const assignAttributes = function (elem, attrs, suspendLength, unitCheck) const ns = (key.substr(0, 4) === 'xml:' ? NS.XML : key.substr(0, 6) === 'xlink:' ? NS.XLINK : null); - + if (isNullish(value)) { + if (ns) { + elem.removeAttributeNS(ns, key); + } else { + elem.removeAttribute(key); + } + continue; + } if (ns) { elem.setAttributeNS(ns, key, value); } else if (!unitCheck) {