From: Marko Luukkainen Date: Thu, 3 Sep 2020 10:09:27 +0000 (+0300) Subject: An attempt to escape generated SVG content. X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=643690b7f2f2a6e7a2eef723fffb5bef0d5fcb34 An attempt to escape generated SVG content. gitlab #596 Change-Id: I11cf5d5836de9b7eb80311c93bd1bfa9ee9705a9 --- diff --git a/bundles/org.simantics.modeling/META-INF/MANIFEST.MF b/bundles/org.simantics.modeling/META-INF/MANIFEST.MF index d23076695..f9d9c2f22 100644 --- a/bundles/org.simantics.modeling/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.modeling/META-INF/MANIFEST.MF @@ -41,7 +41,8 @@ Require-Bundle: org.simantics.simulation;bundle-version="1.0.0", org.simantics.graphfile.ontology, org.apache.batik;bundle-version="1.12.0", org.simantics.graph.compiler, - org.simantics.browsing.ui;bundle-version="1.1.0" + org.simantics.browsing.ui;bundle-version="1.1.0", + org.apache.commons.lang;bundle-version="2.6.0" Export-Package: org.simantics.modeling, org.simantics.modeling.actions, org.simantics.modeling.adapters, diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java index 82115ef0e..559469d4a 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java @@ -723,7 +723,11 @@ public class SCLScenegraph { result.append(ALL_SECTIONS, ""); StringBuilder res = new StringBuilder(); - res.append(""); + if (width > 0 && height > 0 ) { + res.append(""); + } else { + res.append(""); + } res.append(""); res.append(result.get(MAIN_SECTION)); res.append(result.get(SELECTION_SECTION)); @@ -784,15 +788,39 @@ public class SCLScenegraph { private String getKey(SingleElementNode node) { String key; if(node.getKey() != null) { - if (mappings.containsKey(node.getKey())) + if (mappings.containsKey(node.getKey())) { key = mappings.get(node.getKey()).toString(); - else + key = escape(key); + } else { key = node.getKey().toString(); + key = escape(key); + } } else { key = Long.toString(node.getId()); } return key; } + + private String escape(String key) { + // Keys may contain '<' '>' characters, which causes errors in browser SVG handling. + return org.apache.commons.lang.StringEscapeUtils.escapeHtml(key); + } + + private String removeElem(String xml, String elemStart) { + // FIXME: This is rather nasty and error prone way of removing elements from XML string. + // This only supports elements with /> end element tag. Elements ends with are not supported! + int start = xml.indexOf("<"+elemStart); + if (start>=0) { + int end = xml.indexOf(">",start); + if (end >= 0) { + if (start > 0) + return xml.substring(0,start)+xml.substring(end+1); + else + return xml.substring(end+1); + } + } + return null; + } @Override public void enter(IG2DNode node) { @@ -887,7 +915,21 @@ public class SCLScenegraph { parentBuilder.append(MAIN_SECTION, "\n"); } } - parentBuilder.append(MAIN_SECTION, svg.getSVGText()); + String svgContent = svg.getSVGText(); + // SVGNode content may contain SVG/XML elements, which break browser compatibility + int start = svgContent.indexOf("= 0) + svgContent = svgContent.substring(start); + else { + String s = removeElem(svgContent, "?xml"); + if (s != null) { + svgContent = ""+s+""; + s = removeElem(svgContent, "!DOCTYPE"); + if (s != null) + svgContent = s; + } + } + parentBuilder.append(MAIN_SECTION, svgContent); if (!at.isIdentity()) { parentBuilder.append(MAIN_SECTION, "\n"); }