]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
An attempt to escape generated SVG content. 22/4422/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Thu, 3 Sep 2020 10:09:27 +0000 (13:09 +0300)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Thu, 3 Sep 2020 10:09:27 +0000 (13:09 +0300)
gitlab #596

Change-Id: I11cf5d5836de9b7eb80311c93bd1bfa9ee9705a9

bundles/org.simantics.modeling/META-INF/MANIFEST.MF
bundles/org.simantics.modeling/src/org/simantics/modeling/SCLScenegraph.java

index d23076695aeddd71a459ce0a903dd656314f0241..f9d9c2f22f06cc5af220a9c3cebdf893daf43a5d 100644 (file)
@@ -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,
index 82115ef0e0825e2c9b8b2c69793f6bffd015344b..559469d4a21ce908aff65f9574355363a989d2f3 100644 (file)
@@ -723,7 +723,11 @@ public class SCLScenegraph {
                result.append(ALL_SECTIONS, "</g>");
 
                StringBuilder res = new StringBuilder();
-               res.append("<svg width=\"100%\" height=\"100%\" stroke=\"black\">");
+               if (width > 0 && height > 0 ) {
+                       res.append("<svg width=\"" + width +"px\" height=\""+height+"px\" stroke=\"black\" xmlns=\"http://www.w3.org/2000/svg\">");
+               } else {
+                       res.append("<svg width=\"100%\" height=\"100%\" stroke=\"black\" xmlns=\"http://www.w3.org/2000/svg\">");
+               }
                res.append("<g transform=\"matrix(").append(matrix[0]).append(",").append(matrix[1]).append(",").append(matrix[2]).append(",").append(matrix[3]).append(",").append(matrix[4]).append(",").append(matrix[5]).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 </elem name> 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<g transform=\"" + m + "\">");
                      }
                 }
-                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("<svg");
+                if (start >= 0)
+                       svgContent = svgContent.substring(start);
+                else  {
+                       String s = removeElem(svgContent, "?xml");
+                       if (s != null) {
+                               svgContent = "<g>"+s+"</g>";
+                               s = removeElem(svgContent, "!DOCTYPE");
+                               if (s != null)
+                                       svgContent = s;
+                       }
+                }
+                parentBuilder.append(MAIN_SECTION, svgContent);
                 if (!at.isIdentity()) {
                        parentBuilder.append(MAIN_SECTION, "\n</g>");
                 }